yaoyuan 发表于 2013-2-4 22:09:58

Collections and Generics

1. Interface layout
http://www.agoit.com/bbs/../../../upload/attachment/26985/1fe50a9c-8b6b-3d94-8f28-470ec46a9bac.png
2. the .NET platform supports two broad groups of data types, termed value types and reference types. C# provides a very simple mechanism, termed boxing, to convert a value type to a reference
type.
 
3. In summary, generic containers provide the following benefits over their nongeneric counterparts:
• Generics provide better performance, as they do not result in boxing or unboxing penalties.
• Generics are more type safe, as they can only contain the “type of type” you specify.
• Generics greatly reduce the need to build custom collection types, as the base class library provides several prefabricated containers.
 
4. With the introduction of generics, the C# default keyword has been given a dual identity. In addition to its use within a switch construct, it can be used to set a type parameter to its default value. This is clearly helpful given that a generic type does not know the actual placeholders up front and therefore cannot safely assume what the default value will be. The defaults for a type parameter are as follows:
• Numeric values have a default value of 0.
• Reference types have a default value of null.
• Fields of a structure are set to 0 (for value types) or null (for reference types).
// The "default" keyword is overloaded in C#.// When used with generics, it represents the default// value of a type parameter.public void ResetPoint(){    xPos = default(T);    yPos = default(T);} 
5.
// MyGenericClass derives from Object, while// contained items must have a default ctor.public class MyGenericClass<T> where T : new(){...}// MyGenericClass derives from Object, while// contained items must be a class implementing IDrawable// and support a default ctor.public class MyGenericClass<T> where T : class, IDrawable, new(){...}// MyGenericClass derives from MyBase and implements ISomeInterface,// while the contained items must be structures.public class MyGenericClass<T> : MyBase, ISomeInterface where T : struct{...}// <K> must have a default ctor, while <T> must// implement the generic IComparable interface.public class MyGenericClass<K, T> where K : new()where T : IComparable<T>{...}  
页: [1]
查看完整版本: Collections and Generics