Generics in C#
来源:互联网 发布:淘宝联盟官网首页登录 编辑:程序博客网 时间:2024/04/29 06:59
Generics in C# | |||||||
What are generics? Generics permit classes, structs, interfaces, delegates, and methods to be parameterized by the types of data they store and manipulate. Why generics? To well know the useful of generics lets examine the following code:--
We use the object type to store any type of data. The above simple Stack class stores its data in an object array, and its two methods, Push and Pop, use object to accept and return data. While the use of type object makes the Stack class very flexible, it is not without drawbacks. For example, it is possible to push a value of any type, such a Customer instance, onto a stack. However, when a value is retrieved, the result of the Pop method must explicitly be cast back to the appropriate type, which is tedious to write and carries a performance penalty for run-time type checking:
If a value of a value type, such as an int, is passed to the Push method, it is automatically boxed. When the int is later retrieved, it must be unboxed with an explicit type cast:
Such boxing and unboxing operations add performance overhead since they involve dynamic memory allocations and run-time type checks.
While the code above is an improper use of the Stack class, the code is technically speaking correct and a compile-time error is not reported. The problem does not become apparent until the code is executed, at which point an InvalidCastException is thrown.
When the generic class Stack<T> is used, the actual type to substitute for T is specified. In the following example, int is given as the type argument for T:
The Stack<int> type is called a constructed type. In the Stack<int> type, every occurrence of T is replaced with the type argument int. When an instance of Stack<int> is created, the native storage of the items array is an int[] rather than object[], providing substantial storage efficiency compared to the non-generic Stack. Likewise, the Push and Pop methods of a Stack<int> operate on int values, making it a compile-time error to push values of other types onto the stack, and eliminating the need to explicitly cast values back to their original type when theye retrieved.
It was a breif introduction to generics that will be included in the next version of C# (V 2.0) . which is available now on its beta version with Visual C# 2005 Express Edition Beta 1. |
- Generics in C#
- Generics in C#
- C# - Generics
- C#中的Generics
- C# 2.0 - 泛型(Generics)
- [Java学习] Iterator, foreach, generics and callback in Java, C# and Python
- Why generics in method signatures?
- Generics and Wildcards in Java
- C# Programming tips (2) Generics
- Comparing Java and C# Generics
- GENERICS
- Generics
- Generics
- Generics
- Using and Programming Generics in J2SE 5.0
- [zt] More on Generics in the CLR
- Using and Programming Generics in J2SE 5.0
- Generics in the Java Programming Language Java
- JAVAGUIDE(学习笔记1)
- 《重构》语录
- TreeView中如何选中一个父节点同时选中所有的子节点和孙节点。。。
- 持续集成 .Net手册
- funny.exe 木马病毒的手动删除方式
- Generics in C#
- 协作的信息系统风险评估
- ORACLE函数大全
- 朋友
- Struts 的安装、配置与运行
- 奇幻诡异画中画 别让眼睛上了当(1)
- 奇幻诡异画中画 别让眼睛上了当(2)
- 奇幻诡异画中画 别让眼睛上了当(3)
- 奇幻诡异画中画 别让眼睛上了当(4)