Details on the decoration of c classes and members

  • 2020-05-07 20:21:20
  • OfStack

basic concepts :
1. Class is the encapsulation of business processing objects, including the encapsulation of state and behavior.
2. Member type of class:
1. Constant: a symbol whose data value is constant
2. Field: field represents a read-only or read-write data value. Field is usually used to identify a class or a certain state of a class-generated object.
In practice, it is common to identify fields as private to avoid breaking the state of a class/object from outside the class/object.
3. Instance constructor (Constructor) : a special method to initialize the instance fields of a new object to a good initial state.
4. The type constructor, also known as static Constructor, initializes the static fields of the class.
5. Method: a special function to change or query the state of a type or object.
6. Properties: used to encapsulate the field in the external access, avoid direct access to the field.
7. Event: encapsulates a delegate chain, notifies the method execution in the delegate chain through some trigger mechanism.
8. Subtype: a type nested within a type to separate complexity.
9. Operator overloading: redefine specific operations between objects generated by the class: for example +, -, ++, etc.
Conversion operator: defines how to explicitly or implicitly convert a class to another type.

The class modifies the keyword :
1. Accessibility modification :
1. public: for non-nested classes, unlimited access.
2.internal: for non-nested classes, visible only to all the code that defines an assembly.
3.private: for nested classes, visible only to members of the class that contains it.
4.protected: for nested classes, visible only to members of the class A that contains it and the derived class A_Derived of A.
2. Static class modifier
1.static: used to define classes that never need to be instantiated, such as Console, Math, Environment, etc.
2. A static class cannot inherit a base class other than System.Object, nor can it implement an interface
3. Static classes can only define static members
4. Static classes cannot be used as fields, method parameters, or local variables because they represent variables that refer to an instance.
3. Partial class modifier
1.Partial: used to decompose a class or structure into different logical units for the purpose of decomposing the complexity of the type.
2. Common scenarios: when Winform defines a form or control, the front-end unit and logical unit are automatically generated.
4. Components and polymorphic modifiers
1.abstract: indicates an abstract class, which cannot directly generate an instance of the class, and can only be implemented by inheritance.
2.sealed: indicates that this type cannot be used for base types.
3.new: used for a nested type defined in a derived class to indicate that the nested type is independent of the nested type of the same name in the base class.

Modifier keyword for class member
Accessibility: not explained in detail, much the same as the modification of the class
1.public
2.private
3.protected
4.internal
2. Static members: not explained in detail
3. Component and polymorphic modification:
1.abstract: for base class members, indicating that in order to construct an instance of a derived type, the derived type must implement and override this method.
2.virtual: for a base class member, indicating that the member can be overridden by a derived type
3.override: used to derive a class member, indicating that a derived type overrides a member of a base type.
4.sealed: for a base class member, indicating that this member cannot be overridden by a derived type and can only be used for methods.
5.new: used to derive a class member that does not have an overloading relationship with a member of the base class with the same name.

this chapter covers best practices
1. Best practices for accessibility of design classes and members:
1. When defining a class, you should specify Sealed unless you are sure it will be used as the base class.
The reason is that the behavior of derived types is unpredictable, and if the base class is not sealed, a derived class defined in a later release or by another team member may break the state or expected behavior of the base class.
2. When defining a class, you should specify internal if you are not sure that the class will be published outside the assembly.
The reason is also security.
3. Inside the class, all data fields should be defined as private for the reason of state protection. The state of each class/object should be controlled only by itself.
4. Inside a class, don't use virtual unless a method is determined and the property needs to be overridden in a subclass.
There are two reasons. One is that calling virtual methods consumes more performance in CLR. The second is that virtual methods lose the control of their base class over their own behavior.
5. When you define a nested class in a class, you should modify the nested class to private. This rule is mandatory in VS.
2. Similarities and differences between static classes and singletons:
1. Similarities: both provide single 1 entry for type members
2. Differences:
a. Static classes have difficulty in controlling initialization timing
b. Static classes do not support base classes, derived classes, and implementation interfaces.
c. The static class implementation is relatively simple, does not support polymorphism in the case of higher security

Related articles: