"Knowledge has to be improved, challenged, and increased constantly, or it vanishes."

Auto-Implemented properties in C#

When you write applications based on Object Oriented Programming concepts, you need to define lot of classes. When you define classes in C#, you need to define member variables and properties. Usually you define member variables as private and you will expose these member variables with corresponding public properties. This gives you an overhead as you are forced to define private variables just to expose them as properties.

Consider the below example. This defines a class Member with three fields, username, password and email. The below is a typical implementation of such a class.

public class Member
    {
        string _userName;
        public string username
        {
            get
            {
                return _userName;
            }
            set
            {
                _userName = value;
            }
        }

        string _password;
        public string password
        {
            get
            {
                return _password;
            }
            set
            {
                _password = value;
            }
        }

        string _email;
        public string email
        {
            get
            {
                return _email;
            }
            set
            {
                _email = value;
            }
        }

        // …………………remaining implementation including properties and methods..
    }

If you examine the code you will understand, the above code has used the private member variables with names starting with underscore(_) and each member variable has corresponding public properties. In real life situations, the classes will have lot of properties and you need to write lot of code to achieve this.

With C# 3.0, this has been solved with the support of auto-implemented properties. The auto-implemented properties make your code small and easy to write. With auto-implemented properties, you can avoid such member variables you created for properties. From C# 3.0, the above class can be written as below.

  public class Member
    {

        public string username
        {
            get;
            set;
        }

        public string password
        {
            get;
            set;
        }
        public string email
        {
            get;
            set;
        }

        // …………………remaining implementation including properties and methods..
    }

When you define auto implemented property in your class, the C# compiler creates a private, anonymous backing field that can be only accessed through corresponding get and set accessors. You can continue accessing the properties as usual and you save lot of coding time with this. The conventional type of property declaration is still supported and if your application logic require that, you are free to use that method.

reference: http://msdn.microsoft.com/en-us/library/bb384054.aspx 

No Comments