C # get and set usage

Source: Internet
Author: User

Release 1: The accessors of an attribute include executable statements related to the obtained (read or calculated) or set (write) attributes.

The accessors declaration can contain get accessors or set accessors, or both. The declaration takes one of the following forms: get {} set {} get accessors are similar to method bodies. It must return the value of the attribute type. Executing the get accessors is equivalent to reading the field value. The following is the get accesser that returns the value of the private field name:

P rivate string name; // the name field

Public string Name // the Name property

{

Get {return name ;}

}

When a property is referenced, The get accessor is called to read the value of the property unless the property is assigned to the target. Example: Employee e1 = new Employee ();... console. write (e1.Name); // The get accessor is invoked here get accessors must be terminated in The return or throw statements and cannot exceed The accessors. The set accesser method is similar to the method for returning void. It uses an implicit parameter called value. The type of this parameter is the type of the attribute.

In the following example, the set accessors are added to the Name attribute:

Public string Name

{

Get {return name ;}

Set {name = value ;}

}

When assigning values to attributes, call the set accessors with parameters that provide new values. For example, e1.Name = "Joe"; // The set accessor is invoked here is incorrect when using The implicit parameter name (value) for partial variable declaration in The set accessors.

Note:

Attributes are classified according to the accessors used as follows: the attributes with only the get accessors are called read-only attributes. The read-only attribute cannot be assigned a value. The attribute with only the set accessors is called the write-only attribute. You cannot reference a property other than the target of the value assignment. The get and set accessors are read/write attributes. In the attribute Declaration, both get and set accessors must be declared inside the attribute body. Using the get accessors to change the object status is a wrong programming style. For example, the following accessors may cause side effects of changing the object status 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 a field value or calculate a field value. For example:

Public string name

{

Get {return name! = NULL? Name: "Na ";}

}

In the above Code segment, if the name attribute is not assigned, it returns Na. Example 1: This example shows how to access the properties hidden by another property with the same name in the derived class in the base class.

// Property_hiding.cs

// Property hidingusing system;

Public class baseclass

{

P rivate string name;

Public string name

{

Get {return name ;}

Set {name = value ;}

}

}

Public class DerivedClass: BaseClass

{

P rivate 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: JohnName in the base class is: Mary is the focus shown in the previous example: the property Name in the derived class hides the property Name in the base class. In this case, the attribute Declaration of the derived class uses the new modifier: public new string Name {... baseClass is used to access the hidden attributes of the base class: (BaseClass) d1 ). name = "Mary ";

 

Release 2: the code is as follows:

Public class Car

{

P rivate string color;

Public string Color

{

Get {return color ;}

Set {color = value ;}

}

}

In my understanding, using GET and SET to read and write the public variable Color is actually indirectly changing the value of the color private variable. Why does the instance directly perform read/write operations on the color if the color is not set to public? If one day, the boss asked you to change this category to a car Color change and calculate the "price" attribute of the car, then if you directly operate on the Color, aren't you dead? "Attribute" is one of the characteristics of. net. In fact, it is equivalent to a method, especially java usually uses get and set methods (some ideas of. net are java ). The true function of the attribute is not only to change the value of a member variable. For example, the size attribute of form must be re-painted at the same time as set. If you do not want users to modify the color, the set method is not intended for object-oriented set and get. It is generally used to operate variables in the class. instead of directly operating on class variables. A major role is to facilitate maintenance. because: If int a, a variable of a class, is used 1000 times in other packages or namespaces, but after a long time, you want to change a to B, if you operate variable a directly, you need to modify the 1000 of the entire program. if the attribute is used, no more. You only need to modify this method to set public int A {set {a = value;} get {return a ;}}: public int B {set {B = value;} get {return B;} does not need to be changed except for this attribute. I understand it a little bit. Does it enable GET and SET to change the private variables in the class if certain conditions are met. You cannot directly operate the instance. The preceding Code ensures the security of the color attribute. In this case, can it be written as set {color = value * 20; // is the value of Color?} I had the same idea with you at the beginning, but now it has changed. Let's give an example.

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 is difficult to use variables. In addition, you can write multiple statements in get and set. The above get.

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.