Close
Close

C# Properties


Suppose we have a private member in a class. We can use public methods to get and set the value of the variable but C# provides an inbuilt way of doing this - properties.

Properties are just special methods which are used to access private members of a class. Suppose, a Student has a private string name. We can have a property to get and set the value to the variable name. Let's look at the syntax of the property first.

Modifier returnType PropertyName
{
    get
    {
        //return returnType
    }
    set
    {
        //take returnType
    }
}

Here, Modifier is a modifier which can be public, private, protected or internal. returnType is the type of data which we want to set or return to the private member for which we are having the modifier. For example, in the example of Student class, the returnType for the variable name would be string. PropertyName is just the name of the property.

get and set are called accessors becaused they are used to access the private variable.

Let's make a property for the name of Student class.

public string Name
{
    get
    {
        return name
    }
    set
    {
        name = value
    }
}

Here, we have a property named Name which we are using to access the private variable name. Inside get, we are returning the private variable name. In set, we are setting the value of the variable name. set accessor has a free variable value which is created automatically. So, any value passed to the property can be accessed by this variable value.

Suppose, we have an object s of the student class, we can access the Name property using s.Name. So, s.Name would give us the value of the variable name and s.Name = "xyz" would set the value of the variable name to "xyz".

While doing s.Name = "xyz", the value inside the set accessor will be "xyz".

It is similar to creating GetName and SetName methods to return and set the value of the variable name respectively. But C# provides an inbuilt way to do this using properties.

Let's take a working example.

using System;

class Student
{
  private string name;

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

class Test
{
  static void Main(string[] args)
  {
    Student s = new Student();
    s.Name = "xyz";
    Console.WriteLine(s.Name);
  }
}
Output
xyz

We can also access the property of a parent class with the object of its child class. Let's take an example.

using System;

class Student
{
  private string name;

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

class Grad: Student
{

}

class Test
{
  static void Main(string[] args)
  {
    Grad g = new Grad();
    g.Name = "xyz";
    Console.WriteLine(g.Name);
  }
}
Output
xyz

In this example, we accessed the Name property of the parent class (Student) from the object of its child class (g.Name = "xyz").

Overriding Properties in C#


We can also override the property of a parent class from its child class similar to a method. Like methods, we need to use virtual keyword with the property in the parent class and override keyword with the porperty in the child class. Let's take an example.

using System;

class Student
{
  protected string name;

  public virtual string Name
  {
    get
    {
      Console.WriteLine("Parent class get");
      return name;
    }
    set
    {
      Console.WriteLine("Parent class set");
      name = value;
    }
  }
}

class Grad: Student
{
  public override string Name
  {
    get
    {
      Console.WriteLine("Child class get");
      return name;
    }
    set
    {
      Console.WriteLine("Child class set");
      name = value;
    }
  }
}

class Test
{
  static void Main(string[] args)
  {
    Grad g = new Grad();
    g.Name = "xyz";
    Console.WriteLine(g.Name);
  }
}
Output
Child class set
Child class get
xyz

As you can see, we have overriden the Name property of Student class in the Grad class.

C# Static Properties


We can also have a static property for static members. Let's have a look.

using System;

class Student
{
  private static string name;

  public static string Name
  {
    get
    {
      return name;
    }
    set
    {
      name = value;
    }
  }
}

class Test
{
  static void Main(string[] args)
  {
    Student.Name = "xyz";
    Console.WriteLine(Student.Name);
  }
}
Output
xyz

C# Automatic Properties


Take a look at the code sinppet given below.

class Student
{
  private string name;

  public string Name
  {
    get
    {
      return this.name;
    }
    set
    {
      this.name = value;
    }
  }
}

We can reduce the above code as:

class Student
{
  public string Name
  {
    get;
    set;
  }
}

It is called auto-implemented property. Here, a private variable will be automatically created for the property and get and set accessors will function in the same way as they would in the previous example. Let's take an example.

using System;

class Student
{
  public string Name
  {
    get;
    set;
  }
}

class Test
{
  static void Main(string[] args)
  {
    Student s = new Student();
    s.Name = "xyz";
    Console.WriteLine(s.Name);
  }
}
Output
xyz

C# Read-Only Properties


The property which only has a get accessor is known as read-only property because we won't be able to change the value of the variable as we don't have a set accessor.

using System;

class Student
{
  private string name = "xyz";

  public string Name
  {
    get
    {
      return this.name;
    }
  }
}

class Test
{
  static void Main(string[] args)
  {
    Student s = new Student();
    Console.WriteLine(s.Name);
  }
}
Output
xyz

Ask Yours
Post Yours
Doubt? Ask question