1: use attributes to access private members of the category
Always use properties instead of accessible data members.
2: Select readonly instead of const for constants first.
Prefer readonly to const.
Const constants are limited to several basic types and are bound at compilation; readonly constants are bound at runtime.
3: The is or as operator is preferred instead of forced type conversion.
Prefer the is or as operators to casts.
4: Use the Conditional attribute to replace the IF/ENDIF block.
Use Conditional Attributes Instead of # if.
5. always provide a meaningful ToString function.
Always Provide ToString ().
By default, the result returned by the method ToString () that inherits the object is GetType () and then converted to String.
6. Data of different value type and reference type.
Distinguish Between Value Types and Reference Types.
The underlying data is processed by the value type, and complicated operations are included. extended data needs to be processed by the reference type.
7: implement the value type as constant and atomic type as much as possible.
Prefer Immutable Atomic Value Types.
Two methods: 1. Add the readonly flag to all the Members and modify it only in the constructor; 2. Delete the attribute set part.
When an unchangeable atomic value type is implemented (if it includes a reference type branch), it prevents the shortest copy when assigning values to members.
8: Make sure that 0 is the valid status of the value type.
Ensure That 0 Is a Valid State for Value Types
The default value of the value type is 0, but 0 may be meaningless in enum.
9: understand the relationship between the four equal judgments.
Understand the Relationships Among ReferenceEquals (), static Equals (), instance Equals () and operator =.
ReferenceEquals (): determines whether two referenced objects point to the same address;
Static Equals (): Delegate the responsibilities of the comparison to a type being compared (the type of the first parameter );
Instance Equals (): type overload function. Do not overload this operator (boxing overhead) in the value type );
Operator =: type overload function. Do not reload this operator in the reference type;
10: Understand the defects of the GetHashCode () method.
Understand the Pitfalls of GetHashCode ().
GetHashCode () is used to generate a Key for easy retrieval in HashTable or Dictionary. The GetHashCode () of the reference type is actually to return the current reference mark (usually correct, without override); The GetHashCode () of the value type comes () the GetHashCode value of the first Member is used as its return value (generally incorrect ).
It is not recommended to use the GetHashCode function return value of a value type object as the Key of the HashTable object;
The reference type can be used, but note that if the Equals function is rewritten, The GetHashCode function must be rewritten to achieve consistency;
Note the following when rewriting this function:
1. Whether it is a value type or a reference type, make sure that the Members that generate HashCode cannot be modified;
2. Modify the Member that generates the HashCode to generate a new object. At the same time, modify it on the user end, that is, delete the old one and add the new one.
11: Use the foreach loop statement first.
Prefer foreach Loops.
Foreach can generate correct code for arrays and multi-dimensional arrays that have non-zero lower limit for iteration. However, foreach has two restrictions: 1. You cannot modify enumeration members; 2. Do not delete a set.