Thursday, July 28, 2011

New Features in .Net 3.5

(*)New data type var   
The first new feature that is very important for LINQ is the new data type, var. This is a new keyword that can be used to declare a variable and this variable can be initialized to any valid C# data. In the C# 3.0 specification such variables are called implicitly-typed local variables.
   A var variable must be initialized when it is declared. The compile-time type of the initializer expression must not be of null type but the runtime expression can be null. Once it is initialized its data type is fixed to the type of the initial data. The following statements are valid uses of the var keyword:

// valid var statements
var x = "1";
var n = 0;
string s = "string";
var s2 = s;
s2 = null;
string s3 = null;
var s4 = s3;
At compile time, the above var statements are compiled to IL, like this:
string x = "1";
int n = 0;
string s2 = s;
string s4 = s3;

The var keyword is only meaningful to the Visual Studio compiler. The compiled
assembly is actually a valid .NET 2.0 assembly. It doesn't need any special
instructions or libraries to support this feature.
The following statements are invalid usages of the var keyword:
// invalid var statements

var v;
var nu = null;
var v2 = "12"; v2 = 3;
The first one is illegal because it doesn't have an initializer.
The second one initializes variable nu to null, which is not allowed, although once
defined, a var type variable can be assigned null. If you think that at compile
time the compiler needs to create a variable using this type of initializer then you
understand why the initializer can't be null at compile time.
The third one is illegal because, once defined, an integer can't be converted to a string implicitly (v2 is of type string).

(*)Automatic properties
 Hey guys another new and cool feature of .Net 3.0 or above version is the Automatic Properties, In older version of .Net if we wanted to define the member property of class we must had a member variable in class. for example for Product Class, we can define the property ProductName like this:

     Class Product
           string productName;
            public string ProductName
                   get { return productName;}
                   set  {productName = value;}

 But now with the help of .Net3.0 we can define the Member Property of class without having Member Variable.. For example for Product Class we can define the Member Property like this:

  Class Product
             public string ProductName { get; set;}

When Visual studio compiles this statement it will automatically create a private member variable productName This feature can save lot of typing.
.Very cool feature na !!!
Post a Comment

Find a cool free stuff everyday

Giveaway of the Day

Hiren Bharadwa's Posts