c# 3.0 New Features
Introduction:
Microsoft has introduced the C# 3.0 with many key features. It reduces the work of the developers to write many lines of code to achieve the target. It will decrease the code size as well as the additional overhead of the servers.
There are many features like Auto implemented property. Anonymous types, Partial methods, object initializes, implicitly typed local variables.
New Features
1. Auto Implemented Property
The property plays significant role to set and get the values. In the previous versions we were doing like need to set the values to the temporarily local variable. Now that work exactly reduced in the C# 3.0 version. Because it will be implicitly handled by the .Net compiler.
Let's see the new version, Auto implemented property.
[Access-modifier] data type [Name of the property]
{
get;
set;
}
For example we are going to create one property for send email.
public string FromID {get; set ;}
public string ToID {get; set ;}
public string Subject {get; set ;}
public string Message {get; set ;}
In previous version we have done like this.
private string _FirstName;
public string FirstName
{
get
{
return _FirstName;
}
set
{
_FirstName = value;
}
}
Here in C# 3.0, it became auto implemented property, that means in the runtime it will put one temp variable for assign the values by the compiler.
2. Implicit Typed local variable
The new data type introduced in the C# 3.0. Normally when you store the elements in the string with the integer value we need to do the type casting. The variable that will be declared with the var keyword and it will be inferred by the compiler at the time of execution.
var Total = 10 + 10;
var iTotal = 10 + 15.5;
var Name = "Vijay" + "Anand";
var Name = "Anand" + 10;
Here what happens when you declare the var Total = 10 + 10, it will do the addition.
On the other hand, when we declare this variable in the string what happens? Let's see
string name = 10 + 10;
The result will be 1010. Because it will do the concatenation, until unless do the type casting. Here the expression will be inferred by the compiler.
It can be used to create the array in the similar way.
There are some restrictions to use this feature.
We cannot do the increment and decrement operation like i++ or ++i
We cannot declare the NULL value to the var variable.
It use be declare and initialize on the same statement in the local scope.
We cannot initialize the multiple var variables like others. Int I, j, k;
Implicitly Typed Arrays
In the previous section, you have seen how to use the var keyword for implicitly typing variables. In addition to implicitly declaring variables, you can also use the var keyword for declaring arrays as well. For example, consider the following lines of code:
int[] numbers = new int[] { 1, 2, 3, 4, 5};
string[] names = new string[] { "Dave", "Doug", "Jim" };
By using the var keyword, you can rewrite the preceding lines of code as follows:
var numbers = new[] { 1, 2, 3, 4, 5};
var names = new[] { "Dave", Doug, "Jim" };
The compiler infers the type of the array elements at compile-time by examining the values from the initialization expression.
3. Anonymous Types
An anonymous type is one of the new features introduced in the C# 3.0 version. It is a read only property and can be used to assign the set of names constants with the values. These values cannot be changed the outside Anonymous type.
var Names = new
{
FirstName = "Vijay",
LastName = "Anand",
Age = "32"
};
It can be accessed with the following:
string _FirstName = Names.FirstName;
Normally when we define in the enum, there we cannot assign any values.
It will take the order from 0 and will be incremented by one for the every constants in the enum list.
Here the value can be assigned.
Microsoft has introduced the C# 3.0 with many key features. It reduces the work of the developers to write many lines of code to achieve the target. It will decrease the code size as well as the additional overhead of the servers.
There are many features like Auto implemented property. Anonymous types, Partial methods, object initializes, implicitly typed local variables.
New Features
1. Auto Implemented Property
The property plays significant role to set and get the values. In the previous versions we were doing like need to set the values to the temporarily local variable. Now that work exactly reduced in the C# 3.0 version. Because it will be implicitly handled by the .Net compiler.
Let's see the new version, Auto implemented property.
[Access-modifier] data type [Name of the property]
{
get;
set;
}
For example we are going to create one property for send email.
public string FromID {get; set ;}
public string ToID {get; set ;}
public string Subject {get; set ;}
public string Message {get; set ;}
In previous version we have done like this.
private string _FirstName;
public string FirstName
{
get
{
return _FirstName;
}
set
{
_FirstName = value;
}
}
Here in C# 3.0, it became auto implemented property, that means in the runtime it will put one temp variable for assign the values by the compiler.
2. Implicit Typed local variable
The new data type introduced in the C# 3.0. Normally when you store the elements in the string with the integer value we need to do the type casting. The variable that will be declared with the var keyword and it will be inferred by the compiler at the time of execution.
var Total = 10 + 10;
var iTotal = 10 + 15.5;
var Name = "Vijay" + "Anand";
var Name = "Anand" + 10;
Here what happens when you declare the var Total = 10 + 10, it will do the addition.
On the other hand, when we declare this variable in the string what happens? Let's see
string name = 10 + 10;
The result will be 1010. Because it will do the concatenation, until unless do the type casting. Here the expression will be inferred by the compiler.
It can be used to create the array in the similar way.
There are some restrictions to use this feature.
We cannot do the increment and decrement operation like i++ or ++i
We cannot declare the NULL value to the var variable.
It use be declare and initialize on the same statement in the local scope.
We cannot initialize the multiple var variables like others. Int I, j, k;
Implicitly Typed Arrays
In the previous section, you have seen how to use the var keyword for implicitly typing variables. In addition to implicitly declaring variables, you can also use the var keyword for declaring arrays as well. For example, consider the following lines of code:
int[] numbers = new int[] { 1, 2, 3, 4, 5};
string[] names = new string[] { "Dave", "Doug", "Jim" };
By using the var keyword, you can rewrite the preceding lines of code as follows:
var numbers = new[] { 1, 2, 3, 4, 5};
var names = new[] { "Dave", Doug, "Jim" };
The compiler infers the type of the array elements at compile-time by examining the values from the initialization expression.
3. Anonymous Types
An anonymous type is one of the new features introduced in the C# 3.0 version. It is a read only property and can be used to assign the set of names constants with the values. These values cannot be changed the outside Anonymous type.
var Names = new
{
FirstName = "Vijay",
LastName = "Anand",
Age = "32"
};
It can be accessed with the following:
string _FirstName = Names.FirstName;
Normally when we define in the enum, there we cannot assign any values.
It will take the order from 0 and will be incremented by one for the every constants in the enum list.
Here the value can be assigned.
0 comments:
Post a Comment