Some New Features of C# and VB.NET Language


1) Automatic(Auto-Implemented) Properties
2) Object Initializer
3) Type Inference
4) Anonymous Type

========================================================================

AUTOMATIC PROPERTIES

a) It is a new way to define a properties in C#.Net.
b) It define properties in more concise way.
c) When we use automatic property feature, the compiler will create a private and anonymous data  members.
d) Because data members are anonymous and private, whenever we want to assign or retrieve value from these member, we must use the property name.
e) In other words, in class we must have to use the get and set keyword (getter and setter) while we use automatic property feature.
f) Syntax for automatic property is :-
        public int Area{get;set;}

========================================================================

OBJECT INITIALIZER

a) When we use object initializer, we have no need to invoke the object explicitly to initialize the class members.
b) With the help of object intiailize, we can reduce the work amount to initialize the class members.
c) Syntax for object initializer is :-
  Typename referencevariable = new Typename{member=value, member=value};
d) Let us suppose we have class "Room" have member named "Length" and "Breadth".
          Room obj = new Room{Length = 23, Breadth= 35};

========================================================================

TYPE INFERENCE

a) The "var" keyword is used to accomplish the concept of type inference.
b) With the help of type inference, C# compiler determine the data type of the variable at compile time.
c) We have no need to explicity specify the declaration and initialization of variable while we use the type inference.
d) Syntax for type inference is :-
var variablename = value;
e) For example :-
//Right way to use type inference.

var age= 34;   //age is an integer type.
var name = "Amit" //name is a string type.

//Wrong way to use type inference.

var address; //declaration.
address = "#123"; //initialization.
f) One restriction is that we have to declare and initialize at single line.

========================================================================

ANONYMOUS TYPE

a) We use the "new" operator with an object initializer to create an anonymous type.
b) The compiler create a type, that is not available to the end user.
c) With the help of anonymous type we can group the properties without defining it in the class.
d) Syntax for anonymous type is :-
           new {propertyname=value, propertyname=value};
e) Example :-
var result = new {Id="1",Name="Amit",Address="#123"};

========================================================================

Some New Features of C# and VB.NET Language Some New Features of C# and VB.NET Language Reviewed by Baljeet Singh on 10:04 Rating: 5

No comments:

Powered by Blogger.