How to Get and Set in C#

Muhammad Maisam Abbas Feb 16, 2024
How to Get and Set in C#

In this tutorial, we will introduce get and set in C#.

Properties in C#

A property is a public field that provides methods to input, output, and compute the value of a private variable inside a class in C#. The convention for writing a property name is to keep it public and start the name with a capital letter. The following code example shows us how we can write properties in C#.

using System;

namespace Properties {
  class P {
    private string pname;
    public string Name {
      get { return pname; }
      set { pname = value; }
    }
  }
  class Program {
    static void Main(string[] args) {
      P obj = new P();
      obj.Name = "This is his name";
      Console.WriteLine(obj.Name);
    }
  }
}

Output:

This is his name

In the above code, we declared the class P with the private variable pname and a public property Name. The property Name sets and gets the value of the private variable pname. The set and get are used to input and output the value of pname respectively. Both of these are discussed below.

the set Accessor of a Property in C#

The set accessor is used to input the value of a private field in C#. The following code example shows us how we can use the set accessor to input data into a private variable in C#.

using System;

namespace Properties {
  class P {
    private string pname;
    public string Name {
      set { pname = value; }
    }
  }
  class Program {
    static void Main(string[] args) {
      P obj = new P();
      obj.Name = "This is his name";
    }
  }
}

We pass the data into the set accessor by obj.Name = "This is his name" in the main function. This line passes the value This is his name inside the implicit variable value of the set accessor. The value is an implicit variable inside the property which represents the passed value.

the get Accessor of a Property in C#

The get accessor is used to output the value of a private field in C#. The following code example shows us how we can use the get accessor to output data of a private variable in C#.

using System;

namespace Properties {
  class P {
    private string pname;
    public string Name {
      get { return pname; }
      set { pname = value; }
    }
  }
  class Program {
    static void Main(string[] args) {
      P obj = new P();
      obj.Name = "This is his name";
      Console.WriteLine(obj.Name);
    }
  }
}

Output:

This is his name

We accessed the data inside the private variable pname with the public get accessor of the public property Name in C#. We can also write the above code in a shorthand form and still perform the same functionality with the following code.

using System;

namespace Properties {
  class P {
    public string Name { get;

                         set;

    }
  }
  class Program {
    static void Main(string[] args) {
      P obj = new P();
      obj.Name = "This is his name";
      Console.WriteLine(obj.Name);
    }
  }
}

Output:

This is his name

This code does the same thing as the code discussed before it. It gives us a way to access public setters and getters of a private field. In the above code, we pass the value This is his name into the property Name with the set accessor, and get the value with the get accessor.

Muhammad Maisam Abbas avatar Muhammad Maisam Abbas avatar

Maisam is a highly skilled and motivated Data Scientist. He has over 4 years of experience with Python programming language. He loves solving complex problems and sharing his results on the internet.

LinkedIn

Related Article - Csharp Property