Dot Net For All

C# Property With Class Immutability

In this article I will discuss about the C# properties and how we can create immutable class in using C# property with code example. Further I will discuss evolution of properties from C# 1.0 to C# 6.0

Why do we need C# Property

Before exploring about property in C# we need to find what is the requirement of property in C#. Class member variable can be used to define the structure of the class as shown in the below code.

    public class Person
    {
        public string Name;
    }

But there is a problem is the above code where anyone can access and change the Name variable value, after creating an instance of the class.

To overcome the above shortcoming we can write our class as shown in the below figure by making the member variable as private and setting and getting it using methods.

What is C# Property ?

To overcome the verbosity of defining the functions to set and get the member variable of the class properties are introduced in C#. The code example for the property is as shown below

    public class Person
    {
        private string _name;
        public string Name
        {
            get { return _name; }
            set
            {
                if(!string.IsNullOrEmpty(value))
                {
                    _name = value;
                }
            }
        }
    }

The C# compiler generated the get and set methods for the property when the code is changed to IL code. The property is declared like a variables with get and set blocks added. The blocks are changed to get_XXX and set_XXX methods.

Get is used to return the property value and set is used to set the property value. Set accessor runs when property is assigned. The value is the implicit parameter of the property type Like methods we can write logic in get and set code blocks we went want to set and retrieve the property values.

Read and Write Only Property

A property can be read only and write only if we define only get and set code blocks respectively.

Automatic Property in C#

In C# 3..0 automatic properties were introduced. With these properties there is no need to declare a private member variable. These properties instruct the compiler to provide this implementation. The compiler generates a private backing field of a compiler generated name that cannot be referred to.

    public class Person
    {
        public int Name { get; set; }
    }

Class Immutability

A class is known as immutable when it is not changed once it is created. We can create immutable class by declaring all the member variables as private and removing setter blocks for all the properties.

    public class Person
    {
        private string _name;
        public string Name
        {
            get { return _name; }
        }

        public Person(string name)
        {
            _name = name;
        }
    }

One we are developing the above class it looks good. But some other developer comes and add other method named ChangeName as shown below.

        public void ChangeName(string name)
        {
            _name = name;
        }

The above code results in class to be mutable.

For above reason we should declare the private _name member as read-only. This makes the class completely immutable.

Auto-Property Initializers

In C# 6.0 all the pain of declaring a read only private member to set the read-only property and initializing that property has been removed. Auto property initializers has been introduced in C# 6.0. Please check the below figure for implementation of Auto-property initializer.

Auto-Property Initializers

As we can see in the above figure I have created two properties using auto property initializer. Among them one is read only which can be set only in the constructor of the class. Hence auto property initializer can be used to create immutable class. Though above example is not perfect example of immutable class.

Conclusion:

In this article I have discussed about the C# property with code example. We have also seen how properties in C# have evolved since C# 1.0 till C# 6.0.

 

 

Top career enhancing courses you can't miss

My Learning Resource

Excel your system design interview