Generic Interface
You can use generics to define interfaces. methods defined by the mouth can contain generic parameters. In the Linked List Example
Ienunmble <out T> interface, which defines the getenumeratoro method to return ienumtors <out T> .. NET provides many generic interfaces for different situations, such as icompamble, icollection, and extensibleobject. The same interface often has older non-generic versions. For example,. net1.0 has an object-based icomparable interface. Icomparable <int T> is based on a generic type:
Pub1ic interface icomparable <in T>
{
Int compareto (t other );
}
Coordination and resistance to changes
Before. net4, generic interfaces remain unchanged .. Net4 adds an important extension through covariant and anti-converted generic interfaces and generic delegation. The types of parameters and return values are converted. In. net, the parameter type is covariant, And the return type of the method is variable-resistant.
Generic Structure
Similar to a class, the structure can also be generic. They are very similar to generic classes, but do not have inheritance features. This section describes generic
Structure nullable <t>, which is defined by. NET Framework.
A generic structure in. Net fmmework is nullable <t>. The numbers in the database and the numbers in the programming language are explicit.
Because numbers in the database can be empty, and numbers in C # cannot be empty. Int32 is a structure, and its implementation is of the same value type. Therefore, the structure cannot be empty. This problem exists not only in the database, but also in ing XML data to the net type. This difference is often a headache, and data ing requires a lot of assistance. One solution is to map the numbers in the database and XML files to the reference type, because the reference type can be null. But this will also bring additional system overhead during the operation. This problem can be easily solved using the nullable <t> structure.
The structure nullable <t> defines a constraint: the generic type T must be a structure. Define a class as a generic type
And because the class object can be empty, it makes no sense to use the nullable <t> type for the class. Except for the T type defined by nullable <t>, the unique overhead is the hasvalue Boolean field, which determines whether to set the corresponding value or leave it empty. In addition, the generic structure also defines the read-only attributes hasvalue and value, and some operator overloading. To forcibly convert the nullable <t> type to the operator overload of the T type is explicitly defined, because when the hasvalue is false, it throws an exception. The operator overload of the forced conversion to nullable <t> type is defined as implicit, because it can always be converted successfully.
Generic (2)