SoFunction
Updated on 2025-03-07

Deeply explaining get and set in C#

Explanation:
The accessor of an attribute contains executable statements related to fetching (reading or calculating) or setting (write) the attribute. The accessor declaration can include a get accessor or a set accessor, or both. The statement takes one of the following forms:
get {}
set {}
get accessor
The get accessor body is similar to the method body. It must return the value of the attribute type. Execution of the get accessor is equivalent to reading the value of the field. Here is the get accessor that returns the value of the private field name:
Copy the codeThe code is as follows:

private string name;   // the name field
public string Name   // the Name property
{
   get
   {
      return name;
   }
}

When referring to a property, the get accessor is called to read the value of the property unless the property is an assignment target. For example:
Employee e1 = new Employee();
...
();   // The get accessor is invoked here
The get accessor must be terminated in the return or throw statement, and control cannot exceed the accessor body.
set accessor
The set accessor is similar to the method that returns void. It uses an implicit parameter called value, whose type is the type of the property. In the following example, the set accessor is added to the Name property:
Copy the codeThe code is as follows:

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

When assigning values ​​to attributes, the set accessor is called with the parameter that provides the new value. For example:
= "Joe";   // The set accessor is invoked here
It is wrong to use an implicit parameter name (value) for local variable declarations in the set accessor.
Remark
Attributes are classified according to the accessor used as follows:
Properties with only get accessors are called read-only properties. The read-only attribute cannot be assigned.
Properties with only set accessors are called write-only properties. Only write attributes cannot be referenced except as targets for assignments.
The properties with both get and set accessors are read and write properties.
In property declarations, both get and set accessors must be declared inside the property body.
Using the get accessor to change the state of an object is a wrong programming style. For example, the following accessors have side effects of changing the state of the object every time they access the number field.
Copy the codeThe code is as follows:

public int Number
{
   get
   {
      return number++;   // Don't do this
   }
}

The get accessor can be used to return a field value, or to calculate a field value and return it. For example:
Copy the codeThe code is as follows:

public string Name
{
   get
   {
      return name != null ? name : "NA";
   }
}

In the above code snippet, if the Name property is not assigned, it will return the value NA.
Example 1
This example shows how to access a property hidden in a base class with the same name in another property in the derived class.
Copy the codeThe code is as follows:

// property_hiding.cs
// Property hiding
using System;
public class BaseClass
{
   private string name;
   public string Name
   {
      get
      {
         return name;
      }
      set
      {
         name = value;
      }
   }
}
public class DerivedClass : BaseClass
{
   private string name;
   public new string Name   // Notice the use of the new modifier
   {
      get
      {
         return name;
      }
      set
      {
         name = value;
      }
   }
}
public class MainClass
{
   public static void Main()
   {
      DerivedClass d1 = new DerivedClass();
      = "John"; // Derived class property
      ("Name in the derived class is: {0}",);
      ((BaseClass)d1).Name = "Mary"; // Base class property
      ("Name in the base class is: {0}",
         ((BaseClass)d1).Name);  
   }
}

Output
Name in the derived class is: John
Name in the base class is: Mary
The following are the highlights shown in the above example:
Attribute Name in derived class Hides attribute Name in base class. In this case, the attribute declaration of the derived class uses the new modifier:
   public new string Name
   {
   ...
Conversion (BaseClass) is used to access hidden properties in the base class:
   ((BaseClass)d1).Name = "Mary";

Interpretation 2:
The code is as follows:
Copy the codeThe code is as follows:

public class Car
{private string color;
 public string Color
 {
   get
   {return color;
    }
   set
    {color=value;
    }
  }
}

My understanding is: reading and writing to the public variable Color through GET and SET is actually to indirectly change the value of the color private variable, so that's the case. Why not set color as public and let the instance docking and read and write the color?

If one day the boss asks you to change this class to
When the color of the car changes, calculate the "price" attributes of the car at the same time
So if you operate Color directly, aren't you dead?

"Properties" is one of the features of .net.
In fact, it is equivalent to a method, especially in Java, get and set methods are often used (some ideas of .net are java).

The real function of an attribute is not just to change the value of a member variable
For example, the size attribute of form should be redrawn while setting. If you don't want the user to modify the color, don't provide the set method.

Is object-oriented
set and get
Its purpose:Generally, it is to operate on variables in a class, rather than directly operating on variables in a class.
One of the major functions is: it is easy to maintain.
because:
If a variable int a of a class is used 1000 times in other packages or namespace classes, but after a long time, you want to change a to b,
If you operate on variable a directly, you have to modify 1,000 places in the entire program. If you use attributes, it won't be possible. Just change this method
Copy the codeThe code is as follows:

public int A
{
 set
 {
   a = value;
 }
 get
 {
   return a;
 }
}
Put as:
public int B
{
 set
 {
   b = value;
 }
 get
 {
   return b;
 }
}

Nothing elsewhere except this attribute needs to be changed at all
Through the above explanation. I understand a little bit.
Is it possible to satisfy certain conditions that allow GET and SET to change private variables in the class? The instance cannot be operated directly. The code like the above ensures the security of the color attribute.
If that's the case, can it be written
Copy the codeThe code is as follows:

set
{color=value*20;  //Is value equivalent to the value of Color
}

I had the same idea as you at the beginning. But now it has changed.
Let me give you an example to illustrate.
Copy the codeThe code is as follows:

public class Car
{
 public string Color
 {
   get
   {
      if(["color"]!= null)
       {
       return ["color"];
       }
       return "":
    }
   set
    {
      ["color"];=value;
    }
  }
}

This is usually used in this way. If you use variables, it will be difficult to use. Moreover, multiple statements can be written in get and set. As above get.
Don't know if you're satisfied with this explanation?