Directory structure:
contents Structure [+]
- The difference between a property and a field
- No Parameter property
- Auto-implemented Properties
- Object and Collection initializers
- Anonymous type
- System.tuple type
- Have parameter properties
- Accessibility of properties
In this article, the properties are described in detail. There are two types of attributes, one is the parameter attribute (indexer), and the other is the non-parameter attribute.
1. Differences in attributes and fields
Properties and fields (field) are seen by the reader compared to the following code:
classPerson { PublicString M_name;//Field PublicInt32 M_age; PublicString name{//Properties Get{returnM_name;} Set{m_name = value;}//The keyword value represents the new value } PublicInt32 Age {Get{returnM_age;} Set{m_age =value;} } }
As can be seen from the person class defined above, typically, the method that encapsulates the access to a field is the accessor (property).
2. No parameter properties
The parameterless attribute is the same as the property in the person class above, without indexers.
2.1 Auto-implemented properties
C # provides a simpler syntax for parameterless properties, called Auto-implementation properties (automatically implemented property is referred to as API).
For example:
class Person { public String name{set ; Get;} Public Int32 age{get; Set;} }
The above C # automatically declares the field for the name and age property, and it automatically implements the principal method of the name and age property, setting and returning the values in the field, respectively.
2.2 Object and collection initializers
Often you construct an object and set some common properties (or fields) of the object, and C # simplifies this common programming pattern.
As an example of the person class above, in the person class, we can declare the object and initialize the value in one step (through the parameterless constructor):
New Person () {name="jame", age=;
If the attribute implements the IEnumerable or Ienumerable<t> interface, then the attribute is considered to be a collection, for example:
class Classroom { publicsetget;} }
You can then create the classroom object with the following code and initialize the Student property value.
New " Green ","red","blue"};
2.3 Anonymous Types
C # can use anonymous types to declare immutable (immutable) tuple types. Once the type declaration is complete, it cannot be changed.
For example:
var O1 = new {name= " jame " , Age=12 , Sex= male " // o1. Name = "231"; // The compilation does not pass because name is a read-only property. Console.WriteLine (O1. Name); // jame Console.WriteLine (O1. Age); // 12 Console.WriteLine (O1. SEX); // male
The above code, C # automatically creates an anonymous type and contains a read-only property of name, age, and sex.
As you can see from the code above, the syntax for providing anonymous types in C # is:
var o=New {perperty1=expression1,..., perpertyn=expressionn};
C # infers the type of each expression, creates a private field of the type, creates a public read-only property for each field, and creates a constructor to accept all of the expressions.
2.4 System.tuple Type
The system.tuple type is described here because all the properties in the System.tuple type are read-only. System.tuple has several generic versions, the difference being in their number of primitives (the number of generic parameters).
//simplest generic tuple type[Serializable] Public classTuple<t1>{ Private ReadOnlyT1 m_item1; PublicT1 Item1 {Get{returnm_item1;} } PublicTuple (T1 item1) {m_item1=item1; }}//the most complex generic tuple generic type[Serializable] Public classTuple<t1, T2, T3, T4, T5, T6, T7, trest>{ Private ReadOnlyT1 m_item1; Private ReadOnlyT2 m_item2; Private ReadOnlyT3 m_item3; Private ReadOnlyT4 m_item4; Private ReadOnlyT5 M_ITEM5; Private ReadOnlyT6 M_item6; Private ReadOnlyT7 M_item7; Private ReadOnlytrest m_rest; PublicT1 Item1 {Get{returnm_item1;} } PublicT2 ITEM2 {Get{returnm_item2;} } PublicT3 Item3 {Get{returnM_item3;} } PublicT4 ITEM4 {Get{returnM_item4;} } PublicT5 ITEM5 {Get{returnM_ITEM5;} } PublicT6 ITEM6 {Get{returnM_item6;} } PublicT7 Item7 {Get{returnM_item7;} } PublicTrest Rest {Get{returnM_rest;} } PublicTuple (T1 item1, T2 item2, T3 item3, T4 item4, T5 item5, T6 item6, T7 Item7, Trest rest) {m_item1=item1; M_ITEM2=item2; M_item3=Item3; M_ITEM4=item4; M_ITEM5=ITEM5; M_ITEM6=Item6; M_item7=Item7; M_rest=rest; }}
The tuple classes derive from System.Object and implement the IStructuralEquatable, istructuralcomparable interfaces, which are comparable.
Chestnuts:
Static voidMain (string[] args) {Tuple<int32, int32> vals = Minmax ( A, +); Console.WriteLine (Vals. ITEM1);// AConsole.WriteLine (Vals. ITEM2);// +console.readline ();}StaticTuple<int32, int32>Minmax (Int32 A, Int32 b) {return Newtuple<int,int>(Math.min (A, B), Math.max (A, b));}
3. Parameter Properties
We will take the property's get accessor to accept the parameter, which is divided into the parameterless attribute and the parameter attribute (indexer).
The CLR itself does not differentiate between the parameter and the non-parameter attributes. For the CLR, each property is just a pair of methods and metadata for the type definition. Different programming languages use different syntax to create a parameter attribute, C # uses this[...] As the syntax for the expression index, C # only supports defining indexers on an instance of an object.
For example:
classBitArray {Privatebyte[] M_bytearray; PrivateInt32 m_numbits; PublicBitArray (Int32 numbits) {//Validation Arguments if(Numbits <=0) Throw NewArgumentException ("numbits must be > 0"); //number of saved bitsM_numbits =numbits; //assigning bytes to bitsm_bytearray=Newbyte[(numbits+7)/8];//the reason is to add 7, because even if the number of bits is less than 8, there should be one byte. } //The following is an indexer (with parameter properties) PublicBoolean This[Int32 Bitpos] {Get { if(Bitpos <0|| Bitpos >=m_numbits) { Throw NewArgumentException ("Bitpos"); } return(m_bytearray[bitpos/8]& (1<< (bitpos%8)))!=0;//determine if the value of the subscript (bitpos%8) bit is not 0 } Set { if(Bitpos <0|| Bitpos >=m_numbits) { Throw NewArgumentException ("Bitpos"); } if(value) {//sets the bit at the specified index to trueM_bytearray[bitpos/8] = (Byte) (M_bytearray[bitpos/8] | (1<< (bitpos%8)));//Set the value of the subscript (bitpos%8) bit to 1 } Else { //sets the bit at the specified index to falseM_bytearray[bitpos/8] = (Byte) (M_bytearray[bitpos/8] & ~ (1<< (bitpos%8)));//set the value of the bit for subscript (bitpos%8) to 0 } } }}
You can then use the BitArray indexer as follows.
BitArray ba =NewBitArray ( -);//call the set accessor to set all even digits to true for(Int32 x =0; X < -; X + +) {Ba[x]= (x2==0);}//call the get accessor to display the status of all bits. for(Int32 x =0; X < -; X + +) {Console.WriteLine ("Bit"+x+" is"+ (Ba[x]?" on":"Off"));}
4. Accessibility of properties
Sometimes you want to provide an accessibility to the get accessor method, and you specify another accessor for the set accessor method.
For example:
Public class sometype{ private String m_name; Public String name{ get{return m_name;} // default protected Set {M_name=value;} // specified as protected }}
"C #" detailed properties