public class OldPoint
{
private int x,y;
public int X
{
get
{
return x;
}
set
{
x = value;
}
}
public int Y
{
get
{
return y;
}
set
{
y = value;
}
}
}
Now, let us see how easy it is to create a property in C# 3.0. The following code is to create a class called Point with two
properties - X and Y. The underlying variables are automatically crated by C#.
public class Point
{
public int X { get; set; }
public int Y { get; set; }
}
From outside of the class Point, we can access properties X and Y as we accessed properties in the past. So only the way we
create properties changed and not the way we access.
var i = 43; // i is taken as int by compiler
var j = i; // j is taken as same type as i
var s = "...This is only a test...";
var numbers = new[] { 4, 9, 16 };
var complex = new SortedDictionary();
complex.Add("Today", DateTime.Now); // complex is a SortedDictionary where keys are of type string and values DateTime.
Implicitly typed variables make huge difference in LINQ - Language Integrated Query, which allows you to give queries
using your C# language to access Collections or Arrays, XML documents and Databases.
The following example creates an object of Point class and initializes its properties - X and Y - to 10 and 20.
An object of Customer class is created and values are assigned to City and Name properties.
public class Point
{
public int X { get; set; }
public int Y { get; set; }
}
public class Customer
{
public string Name { get; set; }
public string City { get; set; }
}
public static void Main()
{
Point p1 = new Point { X= 10, Y = 20};
List points = new List { new Point { X = 10, Y = 10},
new Point { X = 20, Y = 20} };
Customer c1 = new Customer { City = "Vizag", Name = "Srikanth" };
}
class ExtensionMethodsDemo
{
public static void Main()
{
Customer c1 = new Customer { Name = "Srikanth", City = "Vizag" };
Customer c2 = new Customer { Name = "Srikanth", City = "Visakhapatnam" };
// calls extension method Compare(Customer, Customer) by using c1 and passing c2 as parameter
if (c1.Compare(c2))
Console.WriteLine("Same");
else
Console.WriteLine("Not same");
}
}
static class Extensions // extension method are placed in a static class
{
// extension method that is invoked with a Customer object and has a parameter of Customer type.
public static bool Compare(this Customer c1, Customer c2)
{
if (c1.Name == c2.Name && c1.City == c2.City)
return true;
else
return false;
}
}
public static void Main()
{
List names = new List {"Anders","Scott","Mike"};
foreach (var v in names)
{
var details = new { Name = v, Length = v.Length };
Console.WriteLine(details.Name + "\t" + details.Length);
}
}
A partial method must return void and should not contain any other modifiers or attributes. However it may be static method.
partial class PartialMethod
{
static partial void Print(); // declaration of partial method
static partial void AnotherPrint(); // declaration of partial method. It is not defined in the class.
public static void Main()
{
Print();
Console.WriteLine("In Main");
AnotherPrint(); // call is ignored as partial method is not implemented.
}
static partial void Print() // definition of partial method
{
Console.WriteLine("Print Method");
}
}
Enjoy the new features of C# 3.0. In fact, VB.NET 9.0 has similar features except automatically implemented properties. Of course its syntax is different, sometimes differing by a lot.