`
yaoyuan
  • 浏览: 26262 次
最近访客 更多访客>>
社区版块
存档分类
最新评论

Collections and Generics

    博客分类:
  • .Net
阅读更多

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).

// 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>
{...}
 

 

  • 大小: 50.3 KB
分享到:
评论

相关推荐

    Pro C# 7: With .NET and .NET Core

    Gain a solid foundation in object-oriented development techniques, attributes and reflection, generics and collections as well as numerous advanced topics not found in other texts (such as CIL ...

    Java Generics and Collections (Java泛型与集合)

    本书"Java Generics and Collections"深入探讨了这两个主题,帮助开发者编写更安全、更高效且可维护的代码。 首先,让我们来理解Java泛型。泛型是Java 5引入的一项特性,它允许在类、接口和方法中使用类型参数。这...

    Csharp.6.0.and.the.NET.4.6.Framework.7th.Edition.1484213335.epub

    Readers will gain a solid foundation of object-oriented development techniques, attributes and reflection, generics and collections as well as numerous advanced topics not found in other texts (such ...

    [Java泛型和集合].(Java.Generics.and.Collections).文字版

    本资料 "[Java泛型和集合].(Java.Generics.and.Collections).Maurice.Naftalin&amp;Philip.Wadler.文字版" 由知名专家Maurice Naftalin和Philip Wadler编著,提供了关于这些主题的深入理解。 **Java泛型** 是自Java...

    Java Generics and Collections.chm

    《Java Generics and Collections》是Java开发者必备的参考资料,它深入探讨了Java编程中的泛型(Generics)和集合(Collections)这两个核心概念。在Java编程中,泛型和集合框架是提高代码效率、可读性和类型安全性...

    Pro .NET Performance.pdf

    此外,选择合适的集合类(Collections and Generics)对于性能优化同样重要。.NET框架提供了丰富的集合类,但不同的集合类有其独特的性能特点和适用场景。在决定使用框架内置的集合类或自行实现时,开发者需要对CPU...

    Head First Java In English

    - **第十六章:数据结构——集合和泛型**(Data Structures: Collections and Generics) - 掌握集合框架的核心类和接口。 - 学习如何使用泛型来提高代码的灵活性和安全性。 - 探讨不同类型的集合类的区别。 - **...

    C#入门经典Part1

    7. **集合与泛型(Collections and Generics)**:数组、列表、字典等集合类型是处理数据的重要工具,而泛型提供了类型安全的集合容器。 8. **事件与委托(Events and Delegates)**:C#中的事件处理机制和委托类型...

    C#编程学习笔记_C#编程学习笔记_

    5. **集合与泛型(Collections and Generics)**:了解ArrayList、LinkedList、Dictionary、HashSet等集合类型,以及泛型的概念,能够帮助你有效地存储和操作数据。 6. **LINQ(Language Integrated Query)**:C#...

Global site tag (gtag.js) - Google Analytics