Thursday, May 30, 2013

Read only Properties

As the name suggests read-only properties are those which can be consumed by external classes but cannot be reset by any operation external to the class.

In general a property has get {} and set {} accessors, to create a read-only property we just need to define the get {} accessor for the property without the set {} accessor.

The following example defines a read-only property for the private variable strName

     class Employee
    {
        private string strName;
        //
        public string Name
        {
            get
            {
                return strName;
            }
        }
    }


If we try to assign a value to a read-only property then the compiler will throw the error.

Property or indexer 'XXXX' cannot be assigned to -- it is read only

Search Flipkart Products:
Flipkart.com

No comments: