Table of contents
1.
Introduction
2.
Type of Accessors
3.
Automatic Properties
4.
Advantages of Using Properties
5.
Restrictions on Properties
6.
Frequently Asked Questions
7.
Conclusion
Last Updated: Mar 27, 2024
Easy

Properties of C#

Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

In this article, we will learn about the Properties of C#. Properties enable us to manipulate the private data fields of a class. Properties are those class members that provide a flexible way to get and set data of private fields outside that class.

The most crucial point that we need to remember is that property in C# is never used to store data. It acts as an interface for transferring the data.

Now, we will look at different types of accessors that manipulate the private data fields of a class.

Recommended Topic, Palindrome in C# and Ienumerable vs Iqueryable.

Type of Accessors

There are two types of accessors:

  1. set accessor: It is a method that is used to set data of private data field present in a class. 
  2. get accessor: It is a method that is used to get data of private data fields present in a class.

We will see an example to understand how to get and set the value of private data members.

Example

using System;
public class Employee
   {
       private int emp_id, e_age;
       private string e_name, e_address;
       public int empid
       {
           set
           {
               emp_id = value;
           }
           get
           {
               return emp_id;
           }
       }
       public int eage
       {
           set
           {
              e_age = value;
           }
           get
           {
               return e_age;
           }
       }
       public string ename
       {
           set
           {
               e_name = value;
           }
           get
           {
               return e_name;
           }
       }
       public string eaddress
       {
           set
           {
               e_address = value;
           }
           get
           {
               return e_address;
           }
       }
   }
class Program
{
    static void Main(string[] args)
    {
        Employee emp= new Employee();
        emp.empid = 101;
        emp.ename = "pranaya";
        emp.eage = 27;
        emp.eaddress = "bbsr";
        Console.WriteLine("Employee details are:\n");
        Console.WriteLine("employee id:" + emp.empid+”\n”);
        Console.WriteLine("employee name:" + emp.ename+”\n”);
        Console.WriteLine("employee age:" + emp.eage+”\n”);
        Console.WriteLine("employee address:" + emp.eaddress+”\n”);
        Console.ReadKey();
    }
}

Output

In the above example, we declare the data fields of the Employee class as private. So, these data fields will not be accessible directly outside the Employee class. So, in the Program class, we transferred the data into the data field with the help of properties. As we provide the abstraction to the data fields, this is known as data abstraction. So properties will provide data abstraction.

Automatic Properties

C# also provides automatic properties, where we do not have to define property methods in a class explicitly. We just have to write get and set accessors inside the property.

using System;


class Number
{
  public int Value // property
  { get; set; }
}


class Program
{
  static void Main(string[] args)
  {
    Number myObj = new Number();
    myObj.Value = 3;
    Console.WriteLine(myObj.Value);
  }
}

Output

3

Advantages of Using Properties

  • Properties provide better control of class members and avoid the mess in the code.
  • Properties offer security to the data fields.
  • Properties also validate the data before storing it in the data fields.
  • Fields can be made read-only or write-only using the properties.
  • Properties make the program flexible. Flexibility means the programmer can change one part of the code without affecting other parts.

Restrictions on Properties

  • We cannot overload a property.
  • A property cannot be passed as a ref or out parameter to a method.
  • Property should not alter the state of the underlying variable when the get accessor is called.

Now, let’s move to the FAQ section to clear your doubts related to the C# properties.

Frequently Asked Questions

  1. What are the properties in C#?
    Property in C# is a class member that provides a flexible mechanism for classes to access private fields. C# properties are special methods called accessors. A C# property has two accessors, get property accessor and set property accessor.
  2. Why do we use properties in C#?
    Properties enable a class to show its private data fields to other classes. A get property accessor returns the property value of a private data field, and a set property accessor assigns a new value to the private data member.
  3. Can properties be private in C#?
    Properties can be made public, private, protected, or internal. These access modifiers control how users of the class can access the property. There can be different access modifiers for get and set accessors for the same property.
  4. Can properties be overloaded in C#?
    We cannot overload a property. It means that one can only put a single get and set accessor in a class, respectively.
  5. Can you override properties C#?
    We can override the property of a parent class from its child class, similar to a method. Like methods, we need to use virtual keywords with the property in the parent class and override keywords with the property in the child class.

Conclusion

In this article, we extensively discussed C# properties and why they are used inside a program. We have seen some examples of how to use properties. And at the end of this blog, we learned about the advantages of using properties in C#. 

To read more about reflection in C#, Visit Here.

We hope that this blog has helped you enhance your knowledge regarding Properties in C# and if you want to learn more, check out articles on Coding Ninjas Studio. Do upvote our blogs to help other ninjas grow. 

Happy Coding!

Live masterclass