Consider the bellow Point class
class Point
{
int x, y;
public int X
{
get { return x; }
set { x = value; }
}
public int Y
{
get { return y; }
set { y = value; }
}
}
Then consider the main Program
class Program
{
static void Main(string[] args)
{
Point c1=new Point();
c1.X=10;
c1.Y=100;
Console.WriteLine(c1.X);
Console.WriteLine(c1.Y);
Point c2=new Point {X=20,Y=200}; // See the new type of Initializing
Console.WriteLine(c2.X);
Console.WriteLine(c2.Y);
var p = new Point { X = 30, Y = 300 }; // See the new type of Initializing using "var" keyword
Console.WriteLine(p.X);
Console.WriteLine(p.Y);
Console.ReadLine();
}
}
With complex fields, such as a square or a rectangle whose corners are
located at the points p1 and p2, you can create the Rectangle class as
follows:
public class Rectangle
{
Point p1; Point p2;
public Point ULcorner { get { return p1; } set { p1 = value; } }
public Point LRcorner { get { return p2; } set { p2 = value; } }
}
You can create and initialize the Rectangle object like this:
var rectangle = new Rectangle
{
ULcorner = new Point { X = 0, Y = 0 },
LRcorner = new Point { X = 10, Y = 20 }
};
Note that the semicolon at the end of the object initializer block.
ASP.NET, C#, SQL Server Blog
The ZEN of ASP.NET,C#,SQL Server programming
Categories
- Anonymous Methods (1)
- C# 3.0 Feature (1)
- C# 3.0 Features (1)
- Creating HTML Table Dynamically (1)
- DataView Advanced Filtering with Relationships in C# And ASP.NET (1)
- DataView and DataTable Sorting (1)
- Deserialisation between any objects (1)
- Dynamically Adding Multiple Data Tables Relationships (1)
- Filtering Data in a DataTable (1)
- How to Write Stored Procedures (1)
- Implicitly Typed Local Variables (1)
- Internet Explorer (1)
- Lambda Expressions (1)
- Lamda Expression (1)
- LinQ (1)
- LinQ to XML (1)
- Minimize All (1)
- Object Initializing (1)
- Reading the XML file (1)
- Runnig batch script from C# (1)
- Running Process from C# (1)
- Script Manager (1)
- Serialisation (1)
- SHDocVw (1)
- Sorting with a DataView (1)
- Stored Procedures (1)
- Tips and Tricks VS 2010 (1)
- Update Controls Outside Update Panel With Out Postback (1)
- Update Panel (1)
- Using Two Arguments in a Lambda Expression (1)
- Windows 7 (1)
- XML node traversal (1)
About Me
Followers
Powered by Blogger.
Total Hits
Search
©2008. All rights Reserved
: