Collections and Generics
2008-12-17 11:15
393 查看
1. Interface layout
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).
1. // The "default" keyword is overloaded in C#.
2. // When used with generics, it represents the default
3. // value of a type parameter.
4. public void ResetPoint()
5. {
6. xPos = default(T);
7. yPos = default(T);
8. }
5.
1. // MyGenericClass derives from Object, while
2. // contained items must have a default ctor.
3. public class MyGenericClass<T> where T : new()
4. {...}
5. // MyGenericClass derives from Object, while
946e
6. // contained items must be a class implementing IDrawable
7. // and support a default ctor.
8. public class MyGenericClass<T> where T : class, IDrawable, new()
9. {...}
10. // MyGenericClass derives from MyBase and implements ISomeInterface,
11. // while the contained items must be structures.
12. public class MyGenericClass<T> : MyBase, ISomeInterface where T : struct
13. {...}
14. // <K> must have a default ctor, while <T> must
15. // implement the generic IComparable interface.
16. public class MyGenericClass<K, T> where K : new()
17. where T : IComparable<T>
18. {...}
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).
1. // The "default" keyword is overloaded in C#.
2. // When used with generics, it represents the default
3. // value of a type parameter.
4. public void ResetPoint()
5. {
6. xPos = default(T);
7. yPos = default(T);
8. }
// 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.
1. // MyGenericClass derives from Object, while
2. // contained items must have a default ctor.
3. public class MyGenericClass<T> where T : new()
4. {...}
5. // MyGenericClass derives from Object, while
946e
6. // contained items must be a class implementing IDrawable
7. // and support a default ctor.
8. public class MyGenericClass<T> where T : class, IDrawable, new()
9. {...}
10. // MyGenericClass derives from MyBase and implements ISomeInterface,
11. // while the contained items must be structures.
12. public class MyGenericClass<T> : MyBase, ISomeInterface where T : struct
13. {...}
14. // <K> must have a default ctor, while <T> must
15. // implement the generic IComparable interface.
16. public class MyGenericClass<K, T> where K : new()
17. where T : IComparable<T>
18. {...}
相关文章推荐
- Java Generics and Collections BOOKS
- [转]《Java Generics and Collections》读书笔记二:子类化与通配符
- [转]《Java Generics and Collections》读书笔记三:协变式覆盖与泛型重载
- [转]《Java Generics and Collections》读书笔记四:构造函数,静态成员,嵌套类
- C#数据结构和算法[An Introduction to Collections, Generics, and the Timing Class]
- C#数据结构和算法[An Introduction to Collections, Generics, and the Timing Class]
- 《Java Generics and Collections》读书笔记二:子类化与通配符
- [转]《Java Generics and Collections》读书笔记一:java泛型基本问题
- Section 16 Collections and Generics
- Using and Programming Generics in J2SE 5.0
- 详测 Generics Collections TList (7): Items、Contains
- 详测 Generics Collections TQueue (1): Enqueue、Dequeue、Peek
- Delphi 2009 泛型容器单元(Generics.Collections)[2]: TQueue
- 二十一、详测 Generics Collections TQueue (1): Enqueue、Dequeue、Peek
- 二十九、详测 Generics Collections TDictionary(5): 多种 Create 手段
- Swift from Scratch: Collections and Tuples
- [Java学习] Iterator, foreach, generics and callback in Java, C# and Python
- Differences Between C++ Templates and C# Generics
- Delphi 2009 泛型容器单元(Generics.Collections)[2]: TQueue<T>
- Generics Tutorial – Example Class, Interface, Methods, Wildcards and much more