C # learning notes-interface, set and generic
Luo chaohui (http://www.cnblogs.com/kesalin)
C # And. Net advancedProgramDesign Reading Notes
Interface
1,The interface can be defined as a set of T-member T.The interface can contain methods, attributes, events, and indexers. Implementing interfaces is either completely or completely unnecessary, and cannot partially implement interfaces.
2. Use the as keyword to obtain the interface application.
Person P =NewPerson ("Lilai");
Istudent S = PAsIstudent;
If(S! = NULL)
Console. writeline ("{0} is a student", P. Name );
Else
Console. writeline ("{0} is not a student", P. Name );
We can also use is to determine whether an object has implemented an interface:
If(PIsIstudent)
....
Else
...
3. The interface can be used as a parameter like an object, and the return value is used.
4. implemented through an explicit interface to resolve naming conflicts caused by methods that implement multiple interfaces with the same name. Explicitly implemented members are automatically private, so when using these Members, you must explicitly convert the object to the corresponding interface.
5. interfaces support inheritance and multi-inheritance.
6,Constructing enumeration objects
If the custom type needs to be used with the foreach keyword of C #, the container needs to define a method named getenumerator (), which is customized by the ienumerator interface type. Generally, the implementation of this method is only handed over to the internal Member who saves the sub-object. However, we can also use yieldReturn syntax to provide multiple named iterators"
Method.
7. Callback Interface
The callback interface is generally not directly implemented by an object that is interested in obtaining events, but by a secondary object called a receiver object. The event sender calls the receiver when appropriate. The callback interface is a bit like the delegate in objc.
Set and generic
1. The most basic container is system. array. However, we cannot automatically adjust the array size when adding or deleting elements. We can use the definition in system. the icollection, icomparer, idictionary, idictionaryenumerator, ienumerable, ienumerator, ihashcodeprovider, and ilist interfaces in the collections space implement more flexible containers, or use system. collections. collection of generic container types in the generic namespace <t>, comparer <t>, Dictionary <tkey, tvalue>, list <t>, queue <t>, sorteddictionary <t>, stack <t>, shard list <t>, and readonlycollection <t>.
2. packing and unpacking
The. NET platform supports two types of data: Value Type and reference type. Simply put, packing converts the value type to the reference type, and vice versa. Specifically, packing is to save the variables in system. in the object, the value type display is converted to the corresponding reference type, And the Unbox is to convert the value stored in the object reference back to the corresponding value type on the stack. The unboxing operation must return to the appropriate data type; otherwise, an invalidcastexception occurs.
Packing and packing can help us to access the value type in the container, but it is also cost-effective. Because packing requires creating new objects, we need to destroy objects when removing the box, forced conversion may also cause type security problems. Therefore, we often use generic containers to solve such problems.
3. The generic type in C # is basically the same as that in C ++. C # The default keyword is introduced in the generic model to set the default value of the type parameter. The default value of the value is 0, and the default value of the reference type is null, the field of a structure is set to 0 or null.
4. Use the where keyword in C # to constrain type parameters. The where keyword can be used to control various features of type parameters, but currently does not support operator constraints.
5. A generic class can be used as the base class of other classes. It can define many virtual methods or abstract methods, but it has some limitations on the derived classes of a generic class: A type parameter must be specified for a derived class. If the base class defines a generic virtual or abstract method, the derived type must use the specified type parameter to override the generic method.