Use of get set in C #

Time:2021-10-15

In the process of learning c#, we often encounter such statements:

public string StudentName

{

  get{return stuName;}

  set{stuName=value;}

}

Notes on visual c#. Net programming tutorial: in many object-oriented programming languages, the attributes {student stu = new student(); console. Write (stu. Studentname); console. Readkey();}}}}

The above code defines a property studentname, which contains a get accessor and a set accessor. The attribute studentname encapsulates the field stuname in the class student. If the field is not added with an access control character, it is defaulted to private and cannot be accessed directly by the outside world. Now the outside world can freely access the stuname field through the studentname attribute.

Both get and set of attributes are executable program statement combinations, which have the characteristics of behavior; When using attributes with get accessors and set accessors, it is like using fields, that is, it can accept data as an lvalue and output data as an lvalue. The system automatically selects whether to call get or set according to the position of the attribute in the statement.

Property

There can be only one get and set in the attribute. If there is only get but no set, this attribute can only be read and not written; If there is only set but no get, this property is written only and cannot be read.

Do more in properties

Since get and set are programs, you can certainly do more. A reasonable division of labor is: the design field is to facilitate the use of internal methods and isolate from the outside as much as possible; The design attribute is to facilitate the use of the outside world, but the data not known to the outside world will not be given.

Specific description:

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:

public string Name
{
get
{

  return name; 

}
set
{

  name = value; 

}
}
When an attribute is assigned a value, the set accessor is called with the parameter that provides the new value. For example:

e1.Name = “Joe”; // The set accessor is invoked here
It is wrong to use an implicit parameter name (value) for a local variable declaration in a set accessor.

remarks
Properties are classified according to the accessors used as follows:

A property with only a get accessor is called a read-only property. Cannot assign a value to a read-only property.
A property with only a set accessor is called a write only property. Write only property cannot be referenced except as the target of assignment.
The properties with both get and set accessors are read-write properties.
In a property declaration, 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 the side effect of changing the object state each time they access the number field.

public int Number
{
get
{

  return number++;   // Don't do this

}
}
You can use the get accessor to return a field value, or to calculate a field value and return it. For example:

public string Name
{
get
{

  return name != null ? name : "NA";

}
}
In the above code snippet, if the name attribute is not assigned, it will return 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);   

}
}
output
Name in the derived class is: John
Name in the base class is: Mary
The following are the key points shown in the above example:
The property name in the derived class hides the property name in the base class. In this case, the property declaration of the derived class uses the new modifier:
public new string Name
{

The transformation (baseClass) is used to access hidden properties in the base class:
((BaseClass)d1).Name = “Mary”;


Define a property in the class
public class Student
{
private string name
public string Name
{
Set {name = value;} / / this is to assign a value to the private attribute name
Get {return name;} / / get the value of the private attribute name here
}
}
C# when defining a class, you usually encapsulate the objects declared in the class so that the outside world cannot access this property. In the above code, if the set part is removed, the outside world can only read the value of name. If the get part is removed, only name can be assigned. In this way, the external access to the private attribute name can be controlled. This writing method is a feature of c#.

Of course, you can also create your own functions to value and assign name, but this is more troublesome.

The difference between attribute and ordinary variable is that ordinary variable is what is put in the room. While attribute is that you put a doorman at the door of the house. You take things and put things through him

This gatekeeper is the attribute accessor. Taking things is get, and putting things on is set., and has the final say, even though you feel as if you are putting it directly.
Get set is an external interface. Generally, to access member data in an instance, the access method of this member data is public. Now c# it is very advanced. Get is to obtain and set is to set, but it provides good contractibility. You can set access limits separately for get and set. For example, you can only read or write a member data, Or only derived classes can access… Compared with the past, there is no property accessor and private member data needs to be called through functions. Properties provide efficient access mode and simple writing.
Just for safety~~
Field is the data you want to operate on, so its value can’t make mistakes, but you can’t avoid users making mistakes~~
For security, you can assign values to fields with attributes, because you can verify the security of data in set,
Get is simpler. After verifying the data, you have to give the value to the field, so you need to get
Set means “set” in Chinese;
Get means “get” in Chinese;

Supplement:

Get and set will be called automatically when appropriate
It is mainly used to hide the data structure in the program
Get is to get the value of the property,
Set sets the value of the property.
There is a special variable value when using set
Is the value when setting the property

Value implicit parameter to set accessors and add or remove event handlers.

Set the properties of the class
For example, use class name. Attribute = “” to assign set function
Variable = class name. Attribute value. Get function