1. Implicit local variables
C # 3.0 introduces a new keyword called "var". This keyword allows the developer to create a variable, but does not have to be explicit about its type. For example, use Var to describe a string, like this:
var myData = "This is my data";
Note that there is no mention that the MyData variable is a string, and C # 2.0 asks for it.
Although Var allows you to create an implied type, it does not degrade C # 's strong type characteristics. The VAR keyword is only useful when creating a variable, and once you have established the variable and determined its type, you cannot use Var to change the type of the variable.
For example, this code does not work:
var myDate = DateTime.Now;
myDate = "Hello.";
Using the VAR keyword also produces an interesting result that can help developers reduce the code input when creating variables. For example, to create a customer object in C # 2.0, you would enter the following code:
Customer myCustomer = new Customer();
With the new var keyword, simply enter:
var mycustomer = new Customer ();
Another feature of the Var keyword is that it avoids changing a method call that returns a type object. For example, in C # 2.0, if you need to call a method that returns a Customer object, you should write the following code:
Customer myCustomer = GetByName("Zach");
If a Getbyname method returns an object that is not a customer at some point, the code cannot be compiled. However, if you apply the var keyword, you do not have to worry about the type of object returned by Getbyname.
var myData = GetByName("Zach");
Now, because the VAR keyword is applied, the Getbyname method can be changed to return a person object, and this method call is still valid.
2.extension method
In C #, you cannot inherit and extend a type marked as "encapsulated" with an access identifier. But in C # 3.0, the extension method allows you to extend any class, even a class that is marked as encapsulated. For example, if you want to add a nospaces () method to a string class, we want to define a extension method similar to the one in List A.
List A
namespaceMyExtensionMethods
{
public static class Extension
{
public static void NoSpaces(this string data)
{
return data.Replace(" ", "");
}
}
}