Properties In C# .Net

Introduction

Properties are the unique member of a class. More precisely these are known as the accessors which mean properties are used to access the private data of a class. With the help of properties, we can access private data more safely and in a convenient way. Properties contain two inbuilt methods get and set. The combination of get and set is known as accessors. Get accessor is used to read the value and set accessor is used to write the value in properties. Let's explore this in detail.

What is the get method?

Properties contain an inbuilt get method, which is used to read the value from the class. Get return us a value which we can easily use outside the class irrespective of the access modifier of the data in its origin class.

What is the set method?

Properties contain another inbuilt method known as the set method, which is used to write values in properties. With the help of the value keyword, we will assign the value to the data. 

Syntax for the properties:

<access modifier> <return type> <property name>
{
    private <data type> <variable_name>;
    get
      {
        return <variable_name>;    
      }
    set
      {
         <variable_name> = value;
      }
}

In the above syntax, we will provide the <access modifier> based on the requirement like public, private, protected, and internal. One thing to remember here is that get and set methods cannot have a less restricted modifier than the property itself. Then we have a <return type>, based on the type of data we have. Then we have <property name> which is user define. Inside the braces, we have get and set methods. 

Based upon the get and set methods properties are of four types:

  1. Both Read and Write
  2. Read Only
  3. Write Only
  4. Short Hand(Auto Implemented)

Let's Explore each of them one by one.

1. Both Read and Write

This type of property basically contains both the get and set methods to read and write values respectively. The Order of placing these methods can be changed. Understand this more by an example:

using System;
class Employee {
    private string Empname;
    public string Display {
        set {
            Empname = value;
        }
        get {
            return Empname;
        }
    }
}
class Program {
    public static void Main(string[] args) {
        Employee obj = new Employee();
        obj.Display = "Uhtred";
        Console.WriteLine(obj.Display);
        Console.ReadLine();
    }
}

Output

In the above example, we have a class Employee having a private variable Empname. We cannot access this variable outside the class as this is private. But using a property named Display we can have access to this variable in the class Program. In the set method, we will assign the value keyword to Empname which will get its value during property calling in the class Program. Get method returns us the value of Empname. In the class Program, we created a method for the Employee class and then we called Display by that object.

2. Read Only

This type of property contains only the get method, which is used to get the value. Get method returns us the value and we will assign the value during the variable declaration. We will only use the get method to return the value. Understand this with an example:

using System;
class Employee {
    private string Empname = "Uhtred";
    public string Display {
        get {
            return Empname;
        }
    }
}
class Program {
    public static void Main(string[] args) {
        Employee obj = new Employee();
        Console.WriteLine(obj.Display);
        Console.ReadLine();
    }
}

Output

In the above example, we have only the get method which returns us a value assigned in the same class. We have not assigned the value by an object.

3. Write Only

This type of property contains only the set method, which is used to set the value.

class Employee {
    private string Empname;
    public string Display {
        set {
            Empname = value;
        }
    }
}
class Program {
    public static void Main(string[] args) {
        Employee obj = new Employee();
        obj.Display = "Uhtred";
        Console.ReadLine();
    }
}

In the above example, we have the set method where we have assigned the value to the variable. We have no get method here.

4. Short Hand (Auto Implemented)

This property does not contain the logic part it's a short-hand property and it gets implemented automatically. We will assign the value by the class object to the property and also we will get the value by the object. We do not need to write the block code in the get and set method. We will just terminate them using semicolons. Understand this by an example:

class Employee {
    public string Show {
        get;
        set;
    }
}
class Program {
    public static void Main(string[] args) {
        Employee obj = new Employee();
        obj.Show = "Uhtred";
        Console.WriteLine(obj.Show);
        Console.ReadLine();
    }
}

In the above example, Inside Show property, we have no body of the get and set methods. We have just ended them using semicolons. In the Program class, using the Employee class object we have assigned the value to the property and also read the value using object.

Article in a Nutshell

  • Properties are the unique members of a class used to access the private data members outside the class.
  • Properties contain get and set methods used to access the data.
  • Based upon the get and set properties are of four types.
  • First type of properties have both get and set methods.
  • Second type  of properties have only the get method which is read-only.
  • Third type of properties have only a set method which is write-only.
  • Fourth type of properties are auto-implemented have no logic inside the get and set method.
  • Generally, we use auto-implemented property, which is fast and easy to use.

Thanks for reading this article. I hope, this helped you to grasp the topic of properties.


Similar Articles