Sunday, February 03, 2008

C# 3.0 - New Language Features - Part 3

Automatic Properties

Automatic properties is a feature that reduces the amount of typing that a developer has to perform to implement basic properties - properties that don't do anything other than set or return the value of backing value.

An example of such properties is shown in the following old school code:

class OldPerson
    {
        static public void TestPerson()
        {
            OldPerson testPersonOne = new OldPerson();
            testPersonOne.FirstName = "XYZ";
            testPersonOne.LastName = "abc";

            Console.WriteLine(testPersonOne.FirstName + " " + testPersonOne.LastName);
        }
        private string m_firstName;
        public string FirstName { get { return m_firstName; } set { m_firstName = value; } }
        private string m_lastName;
        public string LastName { get { return m_lastName; } set { m_lastName = value; } }
    }

In the above example - m_firstName and m_lastName are simply present as backing stores for the properties FirstName and LastName. Wouldn't it be nice to be able to just define the properties instead of having to define both the backing store and the properties.

In C# 3.0 this is possible with the feature called "Automatic Properties".

The following is an example of using automatic properties:

class Person
    {
        static public void TestPerson()
        {
            Person testPersonOne = new Person();
            testPersonOne.FirstName = "XYZ";
            testPersonOne.LastName = "abc";

            Console.WriteLine(testPersonOne.FirstName + " " + testPersonOne.LastName);
        }
        public string FirstName { get; set; }
        public string LastName { get; set; }
    }

The FirstName and LastName properties are simply defined by the name of the properties followed by get; set;.

Here are the implementation details:

  • Automatic properties must define both the get and set accessors.
  • The default access level of the set and get accessors is internal.
  • One can define access level on the accessors as shown in the following code, where the set accessor is defined as having only private access and hence the property has read only access from other classes.
    public string FirstName { get; private set; }
    public string LastName { get; private set; }
  • One cannot define the access level on both the accessors of a property: hence the following is illegal
    //even setting both to public is illegal
    public string LastName { private get; private set; }
  • Automatic properties is a compile time feature. What happens is the compiler automatically creates the backing store for you. The backing store is not available to the developer. The name of the compiler generated backing store is random and hence cannot be retrieved using reflection (atleast not easily). It is because the backing store cannot be accessed by the developer - that both the set and get accessors have to be defined on the property.
  • The fact that the developer cannot access the underlying backing store means that they cannot directly mess around with the backing store. So one wont be able to do the following as was done with the OldPerson class:
    public void MessWithPrivateData()
    {
          m_firstName = null;
          m_lastName = null;
    }
  • Automatic properties can return complex types - such as structs and classes, as shown below:
    public OldPerson OldPerson { get; set; }

No comments: