Go into the details of get and set in C

  • 2020-05-12 03:08:12
  • OfStack

Interpretation of 1:
Accessors for properties contain executable statements related to getting (read or calculate) or setting (write) properties. The accessor declaration can contain an get accessor, an set accessor, or both. The declaration shall take the following form 1:
get {}
set {}
get accessor
The get visitor body is similar to the method body. It must return the value of the attribute type. Executing 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:

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

When a property is referenced, the get visitor is called to read the value of the property unless the property is an assignment target. Such as:
Employee e1 = new Employee();
...
Console.Write(e1.Name); // The get accessor is invoked here
The get accessor must terminate in an return or throw statement, and control must not extend beyond the accessor body.
set accessor
The set visitor is similar to returning 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:

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

When a property is assigned, the set accessor is called with the parameter that provides the new value. Such as:
e1.Name = "Joe"; // The set accessor is invoked here
It is an error to use an implicit parameter name (value) for a local variable declaration in the set accessor.
note
Properties are classified according to the accessors used in the following manner:
Properties with only get accessors are called read-only properties. Cannot assign a value to a read-only property.
Properties with only set accessors are called write-only properties. A write-only property cannot be referenced except as a target for assignment.
Properties with both get and set accessors are read and write properties.
In the property declaration, both the get and set accessors must be declared inside the property body.
Changing the state of an object using the get accessor is an incorrect programming style. For example, the following accessors have the side effect of changing object state every time they access the number field.

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

You can use the get accessor to return field values, or to calculate field values and return them. Such as:

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

In the code snippet above, if the Name property is not assigned, it returns the value NA.
Example 1
This example shows how to access a property in a base class that is hidden by another property with the same name in a derived class.

// 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();
      d1.Name = "John"; // Derived class property
      Console.WriteLine("Name in the derived class is: {0}",d1.Name);
      ((BaseClass)d1).Name = "Mary"; // Base class property
      Console.WriteLine("Name in the base class is: {0}",
         ((BaseClass)d1).Name);   
   }
}

The output
Name in the derived class is: John
Name in the base class is: Mary
Here are the highlights from the example above:
Properties Name in derived classes hide properties Name in base classes. In this case, the property declaration for the derived class USES the new modifier:
public new string Name
{
...
The transformation (BaseClass) is used to access the hidden properties in the base class:
((BaseClass)d1).Name = "Mary";

Interpretation of 2:
The code is as follows:

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

My understanding is that by reading and writing the public variable Color via GET and SET, you are indirectly changing the value of the private variable color, in that case. Why not set color to public and let the instance in to read and write color?

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

"Properties" is one of the features of.net.
get and set methods are often used in java (some of the ideas of.net are java).

The real purpose of a property is not just to change the value of a member variable
For example, the size property of form should be redrawn to form at the same time as set. If you do not want the user to modify color, do not provide the set method

It's object-oriented
set and get
It is generally used to operate on variables in a class, rather than directly operating on variables in a class.
There is one big benefit: ease of maintenance.
Because:
If one variable of a class, int a, is used 1000 times in other package or namespace classes, but after a while you want to change a to b,
If you operate directly on the variable a, you will need to modify 1000 parts of the entire program

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

Anything other than this property doesn't need to change at all
Use the above explanation. There's a little bit of that.
Is it possible to satisfy the 1 definite condition and have GET and SET change private variables in the class? You can't let the instance operate directly. Code like the one above guarantees the security of the color property.
Can we write it in that case

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

I had the same idea as you. But that has changed.
So let me give you an example of 1.

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

This is usually used in asp.net. It's hard to use variables. And get,set can write multiple statements. The above get.
I don't know. Is that satisfactory to you?

Related articles: