Friday, April 5, 2013

C# 3.0 : Auto-Implemented Properties

Property provides best way to get and set the value of member fields. It also provides more control while setting and getting value. It means I can check for specific conditions before setting or getting value.



For example I can have property like this:



public int EmployeeAge

{

get

{

return employeeAge;

}

set

{

if (value < 0)

{

//Setting defalut 0 for negative age value.

employeeAge = 0;

}

else

{

employeeAge = value;

}

}

}



Here, we have more control on setting value of member field.



But many times in our program we don't need to check this kind of conditions. We just want to have only get and set assessors. In that case we need to declare property like this:



public int EmployeeAge

{

get

{

return employeeAge;

}

set

{



employeeAge = value;



}

}



This is the minimum code that we need to write. C# 3.0 provides one nice feature called Auto-Implemented Properties to declare a property when no additional logic is required in get and set assessors. 



For example I have class like this:



class Employee

{

int employeeID;

string employeeLName=string.Empty ;

string employeeFName=string.Empty ;

int employeeAge;







public int EmployeeID

{

get

{

return employeeID;

}

set

{

employeeID = value;

}

}

public string EmployeeLName

{

get

{

return employeeLName;

}

set

{

employeeLName = value;

}

}

public string EmployeeFName

{

get

{

return employeeFName;

}

set

{

employeeFName = value;

}

}

public int EmployeeAge

{

get

{

return employeeAge;

}

set

{ 

employeeAge = value; 

}

}

}


Here, I don't need any logic in my properties . I can rewrite my all properties in C# 3.0 in following way.

class Employee

{

int employeeID;

string employeeLName=string.Empty ;

string employeeFName=string.Empty ;

int employeeAge;





public int EmployeeID { get; set; }

public string EmployeeLName { get; set; }

public string EmployeeFName { get; set; }

public int EmployeeAge { get; set; }





}



Doesn't it look good?



There are some constraints that you need to take care when defining auto implemented property.

1) Auto implemented property must declare both get and set assessors. 
2) You can declare attributes for auto implemented property.

Thursday, April 4, 2013

C# 3.0 (LINQ) : Object Initializers

Hi Friends...  

C# 3.0 (LINQ) has got  some nice features and Object Initializers  is one of it.   Here, I am going to describe what is it and where can it be useful to us. 
 
    I have a class called Employee like this :   
public class Employee
    {
        string name;
        string city;

        
        public string Name
        {
            get
            {
                return name;
            }

            set
            {
                name = value;
            }
        }
        public string City 
        {
            get
            {
                return city;
            }

            set
            {
                city = value;
            }
        }

    } 
Now I want to initialize properties of this class.   So, I can have something like this : 
class Program
    {
        static void Main(string[] args)
        {
            //Traditional way 
            Employee emp = new Employee();
            emp.Name = "XXX";
            emp.City = "YYY";

          }  
}
   Here, It will initialize City and Name properties of emp object.   With C# 3.0, I can directly initialize the properties at time of object initialization itself.    I can have something like this :  

class Program
    {
        static void Main(string[] args)
        {         
           // Using Object Initializers
            Employee emp = new Employee {Name = "XXX", City="YYY" };
           
        }  


You can see, how easy it is to initialize the object in C# 3.0 .  And good thing is it provides intelisense window for properties of that class.  I need not to write extra code in order initialize object.  This is a one of the good feature of  C# 3.0 .
Software Development Blogs - BlogCatalog Blog Directory RSS Search Technology Blogs - Blog Rankings Blog Directory