C #: fields and attributes,

Source: Internet
Author: User

C #: fields and attributes,

In MSDN, fields and attributes are described as follows:

A field is a variable of any type that is declared directly in a class or struct.

Field: a field is a variable of any type declared directly in a class or structure.

A property is a member that provides a flexible mechanic to read, write, or compute the value of a private field. properties can be used as if they are public data members, but they are actually special methods called accessors. this enables data to be accessed easily and still helps promote the safety and flexibility of methods.

Attributes are such members: they provide flexible mechanisms to read, write, or calculate private field values. Attributes can be used like public data members, but they are actually special methods called accessors. This allows data to be easily accessed while providing the security and flexibility of the method.

The field is a variable, and the attribute is a method. Fields are generally private, while attributes are generally public. External code can access fields through attributes to perform read and write operations on fields. Therefore, attributes are also called accessors.

Attribute can control the access permissions of external code on fields: only get accessors are implemented to make the Fields read-only; only set accessors are implemented to make the fields write-only; when both get and set accessors are implemented, the field can be read and written externally.

① The property contains both get and set accessors, allowing any object to read and write this property. Any object can also perform read and write operations on the fields corresponding to this attribute.

1 public class Person 2 {3 // ----------------------- 4 // read and write 5 // ------------------------- 6 private string name; 7 // <summary> Name </summary> 8 public string Name 9 {10 get; set; 11} 12}

② The property only contains get accessors. If the set accessors are omitted, the property is read-only. The corresponding external code can only read the fields corresponding to this attribute.

1 public class Person 2 {3 // ----------------------- 4 // read-only 5 // ----------------------- 6 private string name; 7 // <summary> Name </summary> 8 public string Name 9 {10 get; 11} 12}

③ The property only contains the set accesser. If the get accesser is omitted, the property is only written. The corresponding external code can only write the fields corresponding to this attribute.

1 public class Person 2 {3 // ----------------------- 4 // write only 5 // ------------------------- 6 private string name; 7 // <summary> Name </summary> 8 public string Name 9 {10 set; 11} 12}

Attribute to verify the validity of the field write operation.

1 // --------------------- 2 // validity verification 3 // ------------------------- 4 private int age; 5 // <summary> Age </summary> 6 public int age 7 {8 get; 9 set {10 if (value <= 0 | value> = 150) 11 {12 throw new ArgumentOutOfRangeException ("Age", "The range of age is between 1 and 150. "); 13} 14} 15}

C language ^ how to use

A1 = 0x01; // 0000 0001
A2 = 0x00; // 0000 0000
A3 = 0x03; // 0000 0011
A4 = 0x02; // 0000 0010

B1 = a1 ^ a2; // 0000 0001
B2 = a1 ^ a3; // 0000 0010
B3 = a1 ^ a4; // 0000 0011

^ XOR operator. The bitwise value is 0 and the difference is 1. See the example above.

//
Examples of simple and practical problems:
====================================
======= A ======= B =========
There are two circuits on the top. The two switches are a and B respectively. The opening status is \ [1], and the closing status is/[0].
If both circuits are enabled or disabled.
If a turns on [1], B turns off [0], and circuit 1 Powers on
=====================
If a disables [0], B enables [1], and circuit 2 powers on.
====================================
In summary, the circuit fails in the and B states simultaneously [0]. When a and B are different, the power is charged [1].

C language ^ how to use

A1 = 0x01; // 0000 0001
A2 = 0x00; // 0000 0000
A3 = 0x03; // 0000 0011
A4 = 0x02; // 0000 0010

B1 = a1 ^ a2; // 0000 0001
B2 = a1 ^ a3; // 0000 0010
B3 = a1 ^ a4; // 0000 0011

^ XOR operator. The bitwise value is 0 and the difference is 1. See the example above.

//
Examples of simple and practical problems:
====================================
======= A ======= B =========
There are two circuits on the top. The two switches are a and B respectively. The opening status is \ [1], and the closing status is/[0].
If both circuits are enabled or disabled.
If a turns on [1], B turns off [0], and circuit 1 Powers on
=====================
If a disables [0], B enables [1], and circuit 2 powers on.
====================================
In summary, the circuit fails in the and B states simultaneously [0]. When a and B are different, the power is charged [1].

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.