-
How to use C# set and get
The C# language has two functions - an assignment function (get) and a value function (set), which can be clearly seen from the intermediate language code it generates. C# does not advocate setting the domain's protection level to public and allowing users to operate arbitrarily outside the class - that is too unOO, or to be more specific, too unsafe! For all fields that need to be visible outside the class, C# recommends using attributes to express them. Attributes do not represent storage locations, which is the fundamental difference between attributes and domains. The following is a typical attribute design:
using System;
classMyClass
{
int integer;
public int Integer
{
get {return integer;}
set {integer=value;}
}
}
class Test
{
public static void Main()
{
MyClass MyObject=new MyClass();
Console.Write(MyObject.Integer);
MyObject.Integer++;
Console.Write(MyObject.Integer);
}
}
As expected, the program outputs 0 1. We can see that attributes provide programmers with a friendly access interface to domain members by wrapping methods. The value here is the keyword of C#, which is the implicit parameter of set when we perform attribute operations, that is, the rvalue when we perform attribute write operations.
Attributes provide three interface operations: read-only (get), write-only (set), and read-write (get and set). These three operations on the domain must be declared under the same attribute name and cannot be separated. See the following implementation:
classMyClass
{
private string name;
public string Name
{
get { return name; }
}
public string Name
{
set { name = value; }
}
}
The above method of separating the implementation of the Name attribute is wrong! We should put them together like in the previous example. It is worth noting that the three properties (read-only, write-only, read-write) are considered to be the same property name by C#, see the following example:
classMyClass
{
protected int num=0;
public int Num
{
set
{
num=value;
}
}
}
class MyClassDerived: MyClass
{
new public int Num
{
get
{
return num;
}
}
}
class Test
{
public static void Main()
{
MyClassDerived MyObject = new MyClassDerived();
//MyObject.Num= 1; //Error!
((MyClass)MyObject).Num = 1;
}
}
We can see that the attribute Num-get{} in MyClassDerived blocks the definition of the attribute Num-set{} in MyClass.
Of course, attributes are far more than just domain interface operations. The essence of attributes is still methods. We can perform certain checks, warnings and other additional operations based on program logic when extracting or assigning attributes. See the following example:
classMyClass
{
private string name;
public string Name
{
get { return name; }
set
{
if (value==null)
name="Microsoft";
else
name=value;
}
}
}
Due to the nature of the method of attributes, of course attributes also have various modifications of methods. Attributes also have 5 access modifiers, but the access modifiers of attributes are often public, otherwise we will lose the meaning of attributes as public interfaces of classes. In addition to the lack of feature attributes such as method overloading brought by multiple parameters of the method, modifiers such as virtual, sealed, override, and abstract have the same behavior for attributes and methods. However, because attributes are essentially implemented as two methods, it Certain behaviors require our attention. Look at the following example:
abstract class A
{
int y;
public virtual int X
{
get { return 0; }
}
public virtual int Y
{
get { return y; }
set { y = value; }
}
public abstract int Z { get; set; }
}
class B: A
{
int z;
public override int X
{
get { return base.X + 1; }
}
public override int Y
{
set { base.Y = value < 0? 0: value; }
}
public override int Z
{
get { return z; }
set { z = value; }
}
}
This example highlights some typical behavior of properties in the context of inheritance. Here, class A must be declared abstract due to the existence of abstract attribute Z. Subclass B refers to the attributes of parent class A through the base keyword. Class B can overwrite the virtual attributes in class A only through Y-set.
Static properties, like static methods, can only access static domain variables of the class. We can also declare external properties just like we do external methods.