There are several new features introduced in C# 2008. Those are Automatic properties, Object Initializers, Type Inference, Anonymous Types, Lambda Expressions and Extension Methods.
Here we discuss about each feature in detail.
Automatic Properties:
Automatic properties are nothing but shorthand method of normal property. Before .Net 3.5, to create new property you have to define get and set methods as shown below.
string _str;
public string Str
{
get
{
return _str;
}
set
{
_str = value;
}
}
To assign value to property, you have to write code for both getter and setter methods. If you have many properties in your class, it is very difficult for you to define each property. Instead of defining get and set methods, you can define properties very easy and those properties are called automatic properties as shown below.
public string Str { get; set; }
Here we didn’t mention any logic for getter and setter methods, C# compiler will take care about this.
We cannot add any logic to get and set methods and we cannot create read only properties by using Automatic properties. We can reduce amount of code we write to create a property by using Automatic properties.
We can quickly create the automatic property in visual studio by typing prop and hitting Tab key twice.
Object Initializers:
For example, you have a class with several public variables; public properties and you have to pass values to all these fields whenever you are creating the class, then you will write the code as shown below.
class Class1
{
public int i, j;
int a, b;
public int A
{
get { return a; }
set { a = value; }
}
public int B
{
get { return b; }
set { b = value; }
}
}
class Program
{
static void Main(string[] args)
{
Class1 obj = new Class1();
obj.i = 10;
obj.j = 20;
obj.A = 30;
obj.B = 40;
}
}
As shown above, first you have to create instance of class then pass values to all public fields. To create single instance of the class you have to write minimum of five to six lines of code.
You can reduce the number of lines of code by using Object Initializers. Object Initializers reduce the amount of code by passing the values to all public fields while creating the instance of class itself as shown below.
Class2 obj = new Class2() { i=10, j=20, A=30, B=40};
As shown above, you are passing the values to all public fields while creating the instance itself. This reduces the amount of code from five lines to single line.
Anonymous Types:
Sometimes you need to create types dynamically without specifying which type of data it has. In this type scenario Anonymous types are very useful. By using Anonymous Types you can easily create variables without specifying any data type.
var emp = new { Id = 1, Name = "Raj", Location = "U.S" };
As shown above, you are creating the new class with three properties Id, Name and Location and passing the values by using var keyword. Here we didn’t mention any data type for variable emp.
Anonymous Types are very useful while you are working with LINQ.
Type Inference:
Type Inference is new feature that makes C#.Net and Vb.Net dynamic languages like java script by using var keyword. In Type Inference compiler will find type of data at compile time.
var str = "This is Example for Type Inference";
Here we declared the variable str without specifying the data type.C# compiler will find the type of variable str based on its data. You have to povide intial value while while declaring the variable using var to help compiler to find the type of variable based on data, otherwise compiler produces the error.
//This produces the compile time error
var str;
str = "This is Example for Type Inference";
Lambda Expressions:
Lambda expressions reduce the amount of code to define a method. For example you define a method and attach the method with the button click event. You can do this by writing the single line of code using Lambda expressions as shown below.
btn1.Click += (sender, e) => Label.Text = "this is Example for Lambda Expression";
Extension Methods:
If you want to add methods to existing class, you can easily do this by using extension methods. If you want to ass a new method to existing String class, you can add it as shown below.
public static class ExtensionClass
{
public static string AppendString(this string str)
{
return "Extension Method Example: " + str;
}
}
class Program
{
static void Main(string[] args)
{
string str = "hello";
Console.WriteLine(str.AppendString());
Console.ReadLine();
}
}
In the above code we added the new method AppendString which appends some static sting to string variable.