C# Properties Tutorial For Beginners With Examples

In C#, properties are members of a class or struct that expose private fields. Hence, using properties values can be read from or assigned to private fields.
In addition to class and structs, properties can also be part of an interface. This is done using the auto-implemented properties.

Type of Properties in C#

Properties use accessors to read or write data.
Based on the type of operations, properties can be divided into below categories:
  • Read-only property
  • Write-only property
  • Read/Write property

Read-only Property

Read-only properties use the get accessor to read data from fields.
    public class Employee
    {
        private string _language;

        public string Language
        {
            get
            {
                return _language;
            }
        }
    }
        
In the above code _language is the field whose value is exposed to outside world using the property Language.
Language property can be used as shown below.
    Employee emp = new Employee();

    Console.WriteLine("Employee speaks {0}", emp.Language);
        

Write-only Property

Write-only properties use the set accessor to assign value to a field.
    public class Employee
    {
        private string _language;

        public string Language
        {
            set
            {
                _language = value;
            }
        }
    }
        
For set accessor a special value variable is available. This variable contains the value specified by user.
Values can be assigned as shown below:
    Employee emp = new Employee();
    emp.Language = "English";
        

Read/Write Property

Read/Write properties are used to both read and write data. Both get and set accessors are used by these properties.
    public class Employee
    {
        private string _language;

        public string Language
        {
            get
            {
                return _language;
            }
            set
            {
                _language = value;
            }
        }
    }
        
Below example shows how to assign and read value from the same property.
    static void Main(string[] args)
    {
        Employee emp = new Employee();
        emp.Language = "English";

        Console.WriteLine("Employee speaks {0}", emp.Language);

        Console.ReadLine();
    }
        
Output: 
Employee speaks English

Auto-Implemented Properties

Properties which do not need any additional logic can use the Auto-Implemented feature available in C# 3.0 and above.
    public string Age
    {
        get;
        set;
    }
            
These are also called as auto-implemented or automatic properties as there is no need to explicitly declare a private field to store the values. The C# compiler creates these private fields in the background and the automatic property hides this implementation from the user.

Accessibility of Accessors

As described earlier the get or set portions of a C# property are called as accessors and these accessors have the same access level as the property.
Access level can also be set at accessor level by using the access modifier.
    public string Language
    {
        get
        {
            return _language;
        }
        internal set
        {
            _language = value;
        }
    }
        
  • Access modifier can be used only for properties that have both get and set accessors.
  • Access modifier can be used either on get or set accessor and not on both simultaneously.

Why Properties and not Fields or Methods?

So what is the need of a property? Why can't we just use a field or a method instead?
Fields can only be used to assign or retrieve data. It cannot be used to implement any logic before/after performing a data retrieval or assignment. So a property comes into picture to perform somewhat more complex activities which a field cannot do.
In C#, ideally a method is used to perform an action whereas a property represents the feature or data of a class. For example, a class called as Calculator can have methods likeDivideSum and Subtract. All these methods perform some actions. Whereas if the intention is to know the status of the Calculator object before calling any of the above methods then a property called as Status should be exposed.
The benefits of a property are listed below.
  • Properties provide combined functionality of a field and a method.
  • Can be used to validate data before applying or passing the data to any other data element.
  • Can be used to take specific actions like raising events, calling methods, setting values of other properties or fields when a data is changed or accessed.
  • Allows more control over the data. Can be made read-only, write-only or both.
  • Allows a program to apply encapsulation at the accessor level. A public property can have an internal get or set accessor.
There are no extension properties supported by C#. In future versions of C# there is a possibility of this feature to be available.

Properties in Class

A property used in a class can be used to expose the features of a class.
For example a class called as Employee can have properties like NameAge and Location. The below example illustrates this.
    using System;

    namespace tutorialnode
    {
        public struct Employee
        {
            private string _name;
            private int _age;
            private string _location;

            public string Name
            {
                get
                {
                    return _name;
                }
                set
                {
                    _name = value;
                }
            }

            public int Age
            {
                get
                {
                    return _age;
                }
                set
                {
                    _age = value;
                }
            }

            public string Location
            {
                get
                {
                    return _location;
                }
                set
                {
                    _location= value;
                }
            }
        }

        class Program
        {
            public static void Main()
            {
                //Set the values
                Employee employee = new Employee();
                employee.Name = "John";
                employee.Location = "USA";
                employee.Age = 32;

                //Get the values
                Console.WriteLine(employee.Name);
                Console.WriteLine(employee.Age);
                Console.WriteLine(employee.Location);
                Console.ReadKey();
            }
        }
    }        
Output: 
John
32
USA

Properties in Struct

Struct properties behaves in the same way as the class properties. If the previous example is executed by replacing the class keyword with the struct keyword then the output will be the same.

Properties in Interface

Properties can be used in an Interface. Since an interface cannot contain fields so it is not possible to have a regular property in an interface. Instead an interface can have auto-implemented properties. These automatic properties are implemented like any other member of an interface by the implementing class.
In the below example the interface IExample contains an auto-implemented property Color and this property is being implemented by the class Example.
        
    using System;

    namespace tutorialnode
    {
        public interface IExample
        {
            string Color
            {
                get;
                set;
            }
        }

        public class Example : IExample
        {
            string _color;

            public string Color
            {
                get
                {
                    return "Color is " + _color;
                }
                set
                {
                    _color = value;
                }
            }
        }

        class Program
        {
            public static void Main()
            {
                Example example = new Example();
                example.Color = "green";
                Console.WriteLine(example.Color);
                Console.ReadKey();
            }
        }
    }
        
Output: 
Color is green

Abstract Properties

An abstract class can contain abstract properties. These properties do not provide any implementation and must be overridden in the derived class.
Abstract properties may contain both get and set accessors or either one of them.
In the below example the abstract class Utility has an abstract property GetColor. This property has been overridden by the derived class Example.
    using System;

    namespace tutorialnode
    {
        public abstract class Utility
        {
            public abstract string GetColor
            {
                get;
                set;
            }
        }

        public class Example : Utility
        {
            string color = "blue";
            public override string GetColor
            {
                get
                {
                    return color;
                }
                set
                {
                    //Code
                }
            }
        }


        class Program
        {
            public static void Main()
            {
                Example example = new Example();
                Console.WriteLine(example.GetColor);
                Console.ReadKey();
            }
        }
    }    
        
Output: 
blue

Static Properties

A property name with the static modifier is called as a static property. Without a static modifier a property is called as an instance property.
A static property cannot access instance variables of a class. Once a property is declared static the .NET runtime will only retain one copy of the property in the memory. So, if multiple instances of a class is created and the value of a static property is retrieved then it will always get the value set by the last call to this static property.
As shown in the below example, to access a static property the class name is used directly. After the first call to the Age property the value is retained when a second call is made and hence the output is different. If the same example is repeated using a instance property and by using two different instances of the Employee class then the output will be same.
            
    using System;

    namespace tutorialnode
    {
        public struct Employee
        {   
            public static int Age
            {
                get;
                set;
            }
        }

        class Program
        {
            public static void Main()
            {            
                Employee.Age = 32;
                Console.WriteLine(Employee.Age);

                Employee.Age = Employee.Age + 20;
                Console.WriteLine(Employee.Age);

                Console.ReadKey();
            }
        }
    }
            
Output: 
32 52