c sharp

Create Get and Set Methods in C#

The C# programming language provides many facilities to its programmers, one of them is the use of get and set methods. A common word used for them is getters and setters. The concept of getters and setters attains when dealing with sensitive data. Let us make things easy. First, understand what encapsulation is. Encapsulation is hiding sensitive data from the user, and it has a condition we can hide sensitive data from users by defining the variables as private. Private variables are retrieved inside the class. They cannot be retrieved outside that class. But sometimes, we need to use that private data and then utilize the get and set method. These two allow us to access the variables outside that class. Get and set are two methods of property. Property is a mixture of method and variable. Properties are class members that are malleable to read, write, and calculate the value of private data type. These are special methods that can access private variables as they are public.

Why Do We Need the Get and Set Methods?

The need for such kind of method was felt because of two reasons. Those are stated below:

  • Public variables can be used outside the class, and the data can be misused, which is a safety concern.
  • And private variables cannot be accessed outside the class, which ensures security but sometimes, we might need to access private data. In that case, what will we do?

The solution is property, which provides security and safety of data and information. By this, we can access the private variables.

Working

The working is easy, create the class, i.e., class abc, and inside the class, define the variable as private, i.e., private string ab. Then define the same variable public, just change the variable name a little, i.e., capitalize the first letter, which we mostly do public string Ab. And set the curly braces, inside curly braces, create get and set method as get{ return ab; } and set{ ab=value; }

The get{} function will get the same variable that we declared private, and the set{} function will assign value to that variable. From the following samples, it will be easy to comprehend how the get and set methods in C# work.

Example 1

In this example, we will examine how we utilize the get and set methods to access private variables outside the class.

This code will see get and set methods with their usage. We were starting from the class we had created named “Person_info”. We defined a private variable “identification_mark” of string data type inside this class. And define the same variable but this time capitalize the first letter of the variable name “Identification_mark”. Instead of private, set this variable as public. Inside this public method, we will call the get{} and set{} functions. The get{} method will return the required private variable, and the set{} method will set the value, as it is used for assigning value. In the get method, we return the “identification_mark” variable, and in the set method, we specify a value for this variable.

In the Main() method, create the object of the class “Person_info”. We can create multiple objects of one class, but here we created only one object to explain how we can access private variables through get-set methods (property methods). Now, assign a value to the object we created as obj.Identification_mark. Here, Identification_mark is the public variable. The compiler will store the value and then pass it to the identification_mark through the get{} method. Outside the class, the set{} method will assign the value “Mole” to the “identification_mark”. Display a text on the screen with the Console.WriteLine() function. And by utilizing the same function, show the value of the object of a class. Then, call the Console.ReadKey() method to keep the terminal visible otherwise, it will blink and stop.

E:\november\get set\1 out.PNG

Example 2

In this scenario, we will call the get and set methods to access private variables without actually writing manually.

We can use another method for property methods (get and set) without typing the whole code for get and set methods. Just create the class “person_height”. Within this class, we defined a public variable “Height” of integer type. We cannot define the private variable for height here. And inside this method, just write the terms “get” and “set”. These functions will automatically get the variable and then will set the value. We can access the private variable. This is the short method for using get and set methods.

In the Main() method, set the object of a class, i.e., person_height obj=new person_height(); obj shows the created object of the class “person_height”. Through this object, we can call any method or variable of the “person_height” class. Then assign the 165 value to “Height” with the help of “obj”. With console.WriteLine() method, print a message and use another console.WriteLine() to show the defined value of height. The process in the backend is the same as we discussed in the previous code, but it is the shortest method. In the end, utilize the console.ReadKey() to make the output screen on for you.

E:\november\get set\2 out.PNG

Example 3

We would concatenate the values in this code by using the get and set methods.

First, create the class “hello_program”. Then define a private and public variable inside the class. These variables have the same data type, “string”. In the next statement, call the get and set methods. The get{} will return the variable, and set{} will set its value. Here, we concatenated the message “Hello” with the value. We allocate its value to the run time. In the Main() function, construct an object of the class and assign the value “Daniyal Peter” to the object. Represent the name with “Hello” on the screen by using the console.WriteLine() function. After this, invoke the Console.ReadKey() statement.

E:\november\get set\3 out.PNG

Conclusion

In this guide, we examined the get and set methods in detail and the reasons behind our need for them. The property methods (get and set methods) made the data access safe and secure. In C#, we can protect sensitive data from misuse and loss by using these functions. This is data encapsulation. We also talked about the properties, working, and multiple codes of the get and set methods.

About the author

Ann-ul Hayyat

I'm a professional SEO and technical content writer with extensive experience in these fields as well as writing content for blogs, websites, reports, and other topics related to education, science, sports, and many other areas. I am skilled and experienced in creating SEO-friendly blog posts, technical articles, and other content on themes that you may find interesting.