Friday, May 31, 2013

Auto-Implemented Properties

We have seen many types of properties so far, you would have notices that defining a simple get/set property involves many lines of code. A simple private variable with a public property definition needs about 10 lines of code, if this is the case then complex class with many properties will have hundreds of lines of code. Auto Implemented properties help us to overcome this by simplifying property definition of simple properties.

Auto-Implemented Properties were introduced in .Net 3.0 to simplify property definition; the following example defines a 2 auto implemented properties Name and Age with just 2 lines of code.

    class Employee
        public string Name { get; set;}
        public int Age { get; set; }

Notice that we have not defined the underlying private variable to map to the property; this is automatically generated by c#, and can be used with the class object instance as follows.

    Employee objEmployee = new Employee();
    objEmployee.Name = "Test";

    objEmployee.Age = 25;

Search Flipkart Products:

No comments: