Friday, January 9, 2009

C# 3.0 Features

Here I presented Microsoft C# 3.0 features  as Diagrammatic Representation that explains each and every features of C#.

 

image

 

1) Automatic property

     Usually we write Class property as

public class LotDetails
   {
       private int lotNumber;
       private string lotDesc;

       public int LotNumber
       {
           get
           {
               return lotNumber ;
           }
           set
           {
               lotNumber = value;
           }
       }
   }

Now  we can write the above  C# class property as

public class LotDetails
    {
        public int LotNumber { get; set; }
        public string LotDesc { get; set; }
        public string CustomerName { get; set; }
    }

In C# 3.0 compiler will automatically generate the backing field at compile time.

2) Object initializers

which means you can easily assign value to specific properties in a type without having to create an explicit constructor.

Here is the example

let say the above Customer class  we can initialize  as

LotDetails lotdetails = new LotDetails();
lotdetails.LotNumber =343434;
lotdetails.CustomerName ="Alberto";

now  in C# 3.0 we can initialize the object with out creating a constructor.

Customer cust = new Customer {FirstName="Ragavan", LastName="Sreeni"}; 

3) Local Variable type Inference

var int a=10;

var string Name="SeenivasaRagavan";

The var keyword used above shows the use of Implicitly Typed Local Variables for variable assignment

 

4) Query Expressions.

This feature also known as LINQ (Language Integrated Query), it allows you to write SQL-like syntax in C#.

var  lotDetailsRecord = from lotRecord in dbconn.LOT

                                                where lotRecord.LotNumber ==23232

                                                 select lotRecord;

 

5) Lambda Expression.

Lambda expressions are more compact syntax for defining anonymous functions.  The following  code  shows how to use a lambda expression .

string[] Names = {  "Ram","Gandhi","Kalam","Krishna" };

IEnumerable<string> nameLenth = Names.Select(name => name.ToUpper ());

 

Thanks

SreenivasaRagavan.

No comments: