In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article will explain in detail how to use the get and set functions in C#. The editor thinks it is very practical, so I share it with you as a reference. I hope you can get something after reading this article.
C # get set does not advocate setting the protection level of the domain to public to allow users to operate arbitrarily outside the class-that is too unOO or, specifically, too insecure! For all fields that need to be visible outside the class, C# recommends using attributes to express them. Properties do not represent storage locations, which is the fundamental difference between properties and domains. Here is a typical property design:
Using System; class MyClass {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 we expected, the program output 0 1. We can see that properties provide programmers with a friendly access interface for domain members by wrapping methods. The value here is the keyword of C # get set, the implicit parameter of the set when we perform the attribute operation, which is the right value when we perform the attribute write operation.
Property provides read-only (get), write-only (set), read-write (get and set) interface operations. For these three operations of the domain, we must declare them under the same property name, instead of separating them, see the following implementation:
Class MyClass {private string name; public string Name {get {return name;}} public string Name {set {name = value;}
The above method of separating the Name property implementation is wrong! We should put them together as in the previous example. It is worth noting that the three attributes (read-only, write-only, read-write) are considered by C # get set to be the same attribute name, see the following example:
Class MyClass {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 masks the definition of attribute Num-set {} in MyClass.
Of course, attributes are far more than just interface operations of domains. The nature of attributes is still methods. We can perform some additional operations such as checks and warnings when extracting or assigning attributes according to program logic. Take a look at the following example:
Class MyClass {private string name; public string Name {get {return name;} set {if (value==null) name= "Microsoft"; else name=value;}
Because of the nature of the method of the attribute, the attribute also has various modifications of the method. Properties also have five access modifiers, but the access modifiers of properties are often public, otherwise we will lose the meaning of the property as the public interface of the class. Except for the lack of properties such as method overloading caused by multi-parameters of a method, modifiers such as virtual, sealed, override, abstract and so on have the same behavior as the method, but because the attribute is essentially implemented as two methods, some of its behaviors need 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 focuses on some typical behaviors of attributes in the context of inheritance. Here, class A must be declared as abstract because of the existence of the abstract attribute Z. Subclass B refers to the attributes of parent class A through the base keyword. Class B can override the virtual attributes in class An only through Y-set.
Static properties, like static methods, can only access the static domain variables of a class. We can also declare external properties as we do external methods.
This is the end of the article on "how to use get and set functions in C#". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, please share it for more people to see.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.