SET in C #, GET

Source: Internet
Author: User
Tags modifiers

C # in Get and set, it seems to see or not understand, popular point to explain, with what is the advantage, not how
If you write like this, there's nothing different.
private int __old;
public int old{
Get{return __old;}
Set{__old = value;}

}

But if you write like this, it's different.
private int __old;
public int old{
Get{return __old;}
Set{//added the verification code here
if (value<0)
throw new Argumentoutexception ("value", "input value cannot be less than 0");
__old = value;
}

The above example shows that the properties can be added to the code for processing. You can also set access permissions and so on.


Defining a property in a class
public class Student
{
private string Name
public string Name
{
Set{name=value;} Here is the assignment to the private property name
Get{return name;} The value of the private property name is taken out here
}
}
C # When defining a class, it is common to encapsulate objects declared in the class so that the property cannot be accessed by the outside world. If you remove the set part of the code above, the outside world can only read the value of name, and if you remove the get part, you can only assign a value to name. This allows you to control the outside access to the private property name, which is a feature of C # .

Of course you can also create a function to value and assign the name, but this is more troublesome.


The difference between a property and a normal variable is that the normal variable is the thing that is placed in the room, what is plainly. And the property is to put a gatekeeper at the door of the house, you put things to go through him.

The gatekeeper is a property accessor, and the thing is get, and the thing is set. It's up to you how you put it, though you feel like you're just taking it directly.
Get set is an external interface, generally accessing member data in the instance requires this member data access is public, now C # is very advanced, get is GET, set is set, but it provides a good shrinkage, the get and set can be set to separate access permissions For example, you can only read or write to a member's data, or only derived classes may access ... Rather than previously, there is no property accessor, which requires a function to invoke the private member data, which provides an efficient access pattern and simple writing.
It 's for security.
The field is the data you specifically want to manipulate so his value can not be wrong, but you can not avoid users make mistakes ~ ~
Use attributes to assign a value to a field for security, because you can verify the security of the data in set.
Get is much simpler to validate the data and give the value to the field so get
The Chinese meaning of set is "setting";
The Chinese meaning of get is "get";

Add:

Get and set are automatically called at the appropriate time
The main purpose is to hide the data structure in the program.
Get is the value that gets the property,
Set sets the value of this property.
There is a special variable value when using Set
Is the value when setting the property

The value implicit parameter, which sets the accessor and adds or removes an event handler.

To set the properties of a class
For example, use the class name. Property = "" Assignment Set function
variable = class name. property value Get function

--------------------------------------------------------------------------------------------------------------- -------------------------------------------------------------------

The C # language has two functions-an assignment function (get), a value function (set), which can be clearly seen from the intermediate language code it generates. C # does not advocate setting the domain's level of protection to public and allowing users to operate outside of the class--too oo, or too insecure! For all fields that are necessary to be visible outside the class, C # recommends using attributes to express. Property does not represent a storage location, which is the fundamental difference between a property and a domain. The following 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 expect, the program outputs 0 1. We can see that the property provides the programmer with a friendly access interface to the domain member by wrapping the method. The value here is the C # keyword, which is the implicit argument to the set when we perform the property operation, which is the right value when we execute the property write operation.

The properties provide read-only (get), write-only (set), read-write (get and set) three interface operations. For these three operations of the domain, we must declare them under the same property name, and not separate them, and see the following implementations:

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 is wrong! We should put them together as in the previous example. It is worth noting that three attributes (read-only, write-only, read-write) are considered to be the same attribute name in C #, 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 in myclassderived num-get{} masks the definition of property num-set{} in MyClass.

Of course the attribute is far more than just limited to the domain interface operation, the nature of the property is the method, we can according to the program logic in the extraction or assignment of the property of certain checks, warnings and other additional operations, see the following example:

Class MyClass
{
private string name;
public string Name
{
get {return name;}
Set
{
if (value==null)
Name= "Microsoft";
Else
Name=value;
}
}
}

Due to the nature of the properties of the methods, the properties of course also have methods of various modifications. The property also has 5 access modifiers, but the access decoration of the property is often public, otherwise we lose the meaning of the property as the common interface of the class. In addition to the method overloads such as the method overload attribute attributes are not available, virtual, sealed, override, abstract and other modifiers on the properties and methods of the same behavior, but because the property is implemented in essence as two methods, some of its behavior needs 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 of the typical behavior of attributes in the context of inheritance. Here, Class A must be declared abstract because of the existence of an abstraction attribute Z. Subclass B refers to the properties of parent Class A by using the base keyword. A virtual property in Class A can be overridden in class B only by Y-set.

Static properties, like static methods, can only access static domain variables of a class. We can also declare an external property as if it were an external method.

SET in C #, GET

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.