Saturday, November 13, 2010

Auto Implemented Properties


Auto Implemented Properties
In C# 3.0 and later, We have an easy, interesting way for declaring properties known as auto-implemented properties, These Auto implemented properties are especially used when there is no additional logic required in the property accessors. When a property is declared as using Auto Implementation as shown in the example below, compiler creates a private, anonymous backing field or you can say it a member which can be only accessed through the property's get and set accessors.

Example


The following example shows a simple class that has some auto-implemented properties:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace AutoImplemented_Properties
{
    // This class is mutable. Its data can be modified from
    // outside the class.
    class Student
    {
        // Auto-Impl Properties for trivial get and set
        public double TotalMarks { get; set; }
        public string StudentName { get; set; }
        public int RollNo { get; set; }

        // Constructor
        public Student(double marks, string name, int Rno)
        {
            TotalMarks = marks;
            StudentName = name;
            RollNo = Rno;
        }
        // Methods
        public string GetStudentInfo()
        {
            return "Student Information : " + StudentName + " " + RollNo.ToString() + " " + TotalMarks.ToString();
        }
        public string GetMarks()
        {
            return "Marks : " + TotalMarks.ToString();
        }

        // .. Additional methods, events, etc.
    }
}

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace AutoImplemented_Properties
{
    class Program
    {
        static void Main(string[] args)
        {
            // Intialize a new object.
            Student Stud1 = new Student(987.50, "Meetu Choudhary", 90108);
            Console.WriteLine("Marks Before Modification");
            Console.WriteLine(Stud1.GetMarks());
            //Modify a property
            Stud1.TotalMarks += 499.99;
            Console.WriteLine("Marks After Modification");
            Console.WriteLine(Stud1.GetMarks());
            Console.WriteLine("Complete information of Student");
            Console.WriteLine(Stud1.GetStudentInfo());
            Console.ReadLine();
        }
    }
}

Output
Marks Before Modification
Marks : 987.5
Marks After Modification
Marks : 1487.49
Complete information of Student
Student Information : Meetu Choudhary 90108 1487.49


Code Discussion
  • Auto implemented properties are declared using the following line:
public double TotalMarks { get; set; }

·         Value of the property is increased by the object as  it is an Mutable class.

Stud1.TotalMarks += 499.99;

Note:
The class that is shown in the above example is mutable i.e. Client code has the power to change the values of objects after they are created. However, in complex classes that contain significant behavior (methods) and data, it might be a necessary to have public properties. but, for small classes or structs that may just encapsulate a set of values (data) with little or no behaviors (or Methods), it is recommended to make the objects which are immutable by declaring the set accessor as private. Attributes are permitted on auto-implemented properties but not on the backing fields as these fields are not accessible from our source code. If it is necessary to use an attribute on the backing field of a property, then just create a regular property instead of auto implemented property. For more information, see
How to: Implement a Lightweight Class with Auto-Implemented Properties 
To know about properties in C# read this article
For reading on Abstract Properties click here

No comments:

Post a Comment

Subscribe via email

Enter your email address:

Delivered by FeedBurner

MSDotnetMentor

MSDotnetMentor My Website http://msdotnetmentor.com