SoFunction
Updated on 2025-03-07

Specific use of {get;set;} in C#

In C# programs, you often see the use of set and get, and many people don’t know its purpose. I'm here to tell you and deepen my impression.

//There are two classes herepublic class person1
{
    public string name;
}
 
public class person2
{
    public string Name{set;get;}
}

We can see that the name in the first class is not encapsulated, and the second class uses the {get;set;} keyword for encapsulation. In fact, the meaning is also very simple. Get and set correspond to readable and writable respectively. In fact, the previous code was written like this, but the current .net framework can be ignored as above

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

Difference: When instantiating the first "person1" class, the system directly allocates memory to the name attribute when allocating memory space, and then the operation of the name attribute directly operates the block where the name attribute is located; and when instantiating the second person's "person2" type, the system will first allocate a private private memory space called name. Reading and writing operations are carried out through Name, a public pointer-like thing in public, in order to achieve the purpose of encapsulation, and can also control whether it is readable or writable through the get and set keywords.

Simply put, it is to control the read and write permissions of fields.

Let me give you an example:

Suppose that the class is a bank that can save money and withdraw money

    Private Money;
    Private class bank()
    {
      get
      {
         return Money;
      }
      Set
      {
         Money=value;
      }
    }

Money is like an ATM in a bank. You can’t see the Money inside, but you can use set (save money) and get (withdraw money). Money is a private field that is packaged in a class and cannot be directly accessed by programs other than the class. In the usage of get and set in C#, the set and get members of the class are the only way for external programs to access internal properties of the class. Just like when you go to the bank to withdraw money, you cannot directly take money from the bank's safe, but the bank salesperson takes the money out to you.

Attributes look like ordinary variables to the caller, but as a class designer, you can use attributes to hide some fields in your class, so that the outside world can only access your fields through attributes. You can use attributes to restrict the outside world's access to your fields, so you can use get and set. If you want the user to access your fields at will, then implement get and set; if you want the user to read the fields, only implement get; if you want the user to write fields, only implement set. You can also do some verification work on the values ​​passed by the user in set and get to make sure your field will contain the correct value.

    Private int a;
    Public int index
    {
      get
      {
        return a;
      }
      set
      {
         If (value>0)
           a=value;
         else
           a=0;
      }
    }

It can be seen that one of the usages of get and set is to hide the real members inside the component or the class;

The second is used to establish constraints, such as implementing constraints such as "I have no you";

The third is used to respond to attribute change events. When attribute change is to do something, just write it in the set method.

When you want to read or write the value of the property, the access flag defines the implemented statement. The access flag used to read out the value of the attribute is marked as the keyword get, while the read and write flag of the value of the attribute is to be modified is marked as set.

This is the introduction to this article about the specific use of {get;set;} in C#. For more related C# {get;set;} content, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!