c sharp

C# Getter Setter

Everything contains distinct properties from which we can differentiate it from others. Just like that, we use the concept of property in C# programming. If you have ever heard of encapsulation, then you already knew that we tend to hide sensitive data or information from some users in the encapsulation process. Within the encapsulation process, we tend to use the public getter and setter function to fetch and update some private field values using the property concept. Thus, we will discuss the use of getter setter functions in C# programs to perform encapsulation at different inheritance levels. Let’s get started with the shell terminal for the creation of the C# file to make our C# code in it. Use the “touch” query as follows:

Example # 01:

Within this example, we will see how getter and setter functions help us in fetching and updating some private data members of one class to another without any error. We will be starting our code with the “System” library usage. This is a must-have step in C# coding. After that, we created a Test namespace in this code and initialized two classes (i.e. Test and New.)

The Test class contains a property and a New class contains a Main() driver function in this code. So, we will be starting from the Test class. This test class has been started with the declaration of an integer variable “age” of private type which cannot be accessed outside of the Test class. After this, we have been using the property “Age” of the same name as a private member but the first letter is capital. This property must be of the same type and public access modifier. The property Age is a group of methods and a private variable. It contains a get and set function in it.

The get function has been used to return the value of the private variable “age” and the set function is used to set the value of the private variable “age” with the “value” keyword. For the main() method, it contains the initialization of Test class object “t” with the class name followed by the “new” keyword. Now, we have been using this object “t” of class Test to set the value “14” for property “Age” or indirectly for private variable “age”. Then, we have been using the “Console.WriteLine()” function statement here to display the value of the private variable “age” by using the property “Age” called by the object “t”. Our code is now completed and ready for our use. Let’s save it first with Ctrl+S and go back to the terminal to compile it.

We have been utilizing the C# compiler “mcs” that is already installed in our Ubuntu 20.04 system for the compilation of files. After the successful compilation of this code, we have been using the C# executor “mono” runtime at our shell to execute the “exe” file generated by the compilation process. The output of this execution shows the value for “age” is 14. This means that the get and set function can be utilized within any property of public type to get access to some private data members from another class.

Example # 02:

Within this example, we will be using the C# expression concept to give the single line definition to getter and setter functions. We started this code with the public class Cat containing the declaration of 1 public static variable “c” and two private variables “age” of int type and “title” of string type. We have created a read-write property “Title” with the same name to “title” private member and using the get and set functions between it.

We have been using expression statements to define get and set to return value or private variable ‘title’ and assign it a value. After this property, we have defined a read-only property “Age” of static type using the expression statement to set the value of the private integer variable “age”. The construct of class “Cat” has been defined that has been using the expression statement to set the value of private variable “age” using the public variable “c” of integer type pre-incrementing the value of “c” and assigned to “age”. Within the Main() function of a New class, the Cat class name has been used to set the value of public variable “c” as “4”. Then, we created an object “t” for the Cat class. When the object is created, a constructor of the Cat class will be executed and increment the value of “c” by 1 and store it to the variable “age”. We have used the object “t” to set the value for the “Title” property i.e. “Persian”. After this, we have been using the Console.WriteLine() statement to display the value of the “age” variable using the property “Age” that is ready-only it will then return the value of “age” to the main. The next statement is using the Title property to display the value of the private variable “title”.

After compiling and running this code, the age of the cat is displayed as “5” after the increment of the “c” variable stored in the “age” variable. The Title has been displayed as “Persian” for the private member “title” using the property “Title”.

Example # 03:

Let’s have a last example to see how to get and set functions work within the inheritance of classes while working in C#. Thus, we have updated our code and added three classes to it. The Dog class has been derived from the Cat class (i.e. inheritance.) While the new class is not part of this inheritance and works independently. Both the Parent class “Cat” and the child class “Dog” contain the same private string type variable “title” and the public string type property with the first letter in upper case i.e. “Title”.

Within the same name property “Title” of both functions, the get and set functions have been utilized to set the value of the private variable “title” and return to the main. The Dog class property contains a little change in this property using the “Shephard” keyword along with the values passed by the main() method. Here comes the Main() function of the New class initializing object “d” of child class Dog. This object has been used to set the value of a property “Title” from the Child class of this program (i.e. Dog.)

To set the property “Title” of a Cat (parent) class, we need to use the name of a class “Cat” along with the child class object calling the property “Title”. After this, we have used the object “d” to display the value of Dog class property variable values and the Cat class name with the object “d” to call the Cat class property variable via the Console.WriteLine() function.

After executing this code, we have the child class value first and then the Cat class value set by the property “Title”.


This is about the use of getter and setter functions in C# . We have seen how we can make use of them while encapsulating the data members of a class and use the concept of properties in our codes as well. We have utilized the expression statements to set the values for private data members using the properties. Then, we have discussed how a private data member can be accessed with the inheritance while using “get” and “set” expression statements in the properties.

About the author

Aqsa Yasin

I am a self-motivated information technology professional with a passion for writing. I am a technical writer and love to write for all Linux flavors and Windows.