Chapter 4th of. NET Design Specifications: type design specifications and. net Design Specifications
Chapter 2: type design specifications 4th types and namespaces
A namespace is used to organize a type into a hierarchy composed of the relevant functional areas.
Avoid deep namespaces. Because users need to search back frequently, it is very difficult to browse such layers.
Avoid too many namespaces.
Avoid placing the types designed for advanced solutions and those designed for common programming tasks in the same namespace.
Do not define a type without specifying a type namespace.
Put the types that provide Design-time functions for basic namespaces in namespaces with the suffix ". Design.
Put the types that provide custom Permissions for the basic namespace in The namespace with the suffix ". Permissions.
Put the types that provide interoperability for basic namespaces in namespaces with the ". Interop" suffix.
Put all the code in the main interoperability assembly in a namespace with the suffix ". Interop.
4.2 select between classes and structures
Consider defining the structure instead of defining the class-if the type of instance is small and the declaration cycle is short, or it is often embedded in other objects.
Avoid defining the structure unless the type has all of the following features.
It logically represents an independent value, similar to basic types (int, double, etc;
The instance size is smaller than 16 bytes;
It is immutable;
It does not need to be packed frequently.
4.3 select between classes and interfaces
Class rather than interface should be prioritized
Abstract classes instead of interfaces are used to remove the coupling between contracts and implementations.
Interface to define-if you need to provide the value type of the multi-State hierarchy.
Consider defining interfaces to achieve the same effect as multi-inheritance.
4.4 Design of abstract classes
Do not define public or internally protected constructors in abstract types.
To define a protected constructor or internal constructor for an abstract class.
Each abstract class must be provided with at least one specific type inherited from the abstract class.
4.5 static class design
Use as few static classes as possible.
Do not use static classes as a sandbox.
Do not declare or override instance members in static classes.
Static classes should be defined as sealed and abstract, and a private instance constructor should be added-if the programming language does not directly support static classes.
4.6 Interface Design
Interface to define-if you want to make a group of types including value types support some public APIs.
Consider defining interfaces-if you want to allow the types that have inherited from other base classes to support the functions provided by this interface.
Avoid using the mark interface (interfaces without members ).
You must provide at least one type for this interface.
Each interface must be provided with at least one API that uses this interface (a method that uses this interface as a parameter or an attribute of the interface type ).
Do not add members to released interfaces.
4.7 Structure Design
Do not provide Default constructors for the structure.
Do not define variable value types.
Make sure that the structure is still valid when all data of the schema instance is 0, false, or null (if appropriate.
To implement IEquatable for the value type <T>.
Do not explicitly extend System. ValueType. In fact, most programming languages do not allow this.
4.8 enumeration Design
Use enumeration to enhance the type of parameters, attributes, and returned values of the set that represent values.
Use enumeration instead of static constants.
Do not use enumeration in an open set (such as an operating system version or a friend's name ).
Do not provide enumerated values reserved for future use.
Avoid exposing only one enumerated value in the display area.
Do not include sentinel values in enumeration.
To provide zero value for a simple enumeration type.
Consider Int32 as the carrier for enumeration (the default choice for most programming languages), unless any of the following is true.
You must use a plural noun or a noun phrase to name a tag enumeration, and use a singular noun or a noun phrase to name a simple enumeration.
Do not directly expand System. Enum.
Use System. FlagsAttribute for tag enumeration. Do not use this modifier for simple enumeration.
Use the power of 2 as the enumerated value, so that you can use the bitwise OR operation to freely combine them.
To provide special enumerated values for common tag combinations.
Avoid making the created tag enumeration contain invalid combinations.
Do not use 0 as the flag enumeration value unless it indicates that "All tags are cleared" and are named properly according to the next specification.
Name the 0 value marked with enumeration as None. For tag enumeration, this value must always indicate "All tags are cleared ".
Consider adding values to the enumeration, despite the risk of compatibility.
4.9 nested type
The nested type is used only when you want to allow a type to access members of the outer type.
Do not use public nested types for logical grouping. Use namespaces for this purpose.
Avoid exposing nested types publicly. Unless you declare a variable of the nested type in rare cases, such as when a subclass is derived or in other advanced scenarios that need to be customized.
Do not use nested type-if this type may be referenced by a type other than its outer type.
Do not use nesting-if you need Customer Code to instantiate them. If a type has a public constructor, it should not be nested in other types.
Do not define nested types as interface members. Many programming languages do not support this.
4.10 type and Assembly metadata
The CLSCompliant (true) attribute must be used in programs that contain common types.
You need to use AssemblyVersionAttribute to modify attributes in a program that contains public types.
Use the format of <V>, <S>, <B>, and <R> in the assembly version number. V indicates the master version, S indicates the service version, B indicates the build number, and R indicates the build revision number.
You need to use the following modifier attribute in the Assembly to provide additional information.
Consider using ComVisible (false) in the Assembly ). APIS that can be called by COM must be specially designed.
The purpose of using AssemblyFileVersionAttribute and AssemblyCopyrightAttribut in an assembly is to provide additional information related to the Assembly.