上一篇:用Visual C#实现文件下载功能 >>
Generics in C#
Generics are the most useful C# 2.0 language extensions, beside Anonymous methods, Iterators, Partial types And Nullable types.
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:--
public class Stack
{
object[] items;
int count;
public void Push(object item) {...}
public object Pop() {...}
}
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:
Stack stack = new Stack();
stack.Push(new Customer());
Customer c = (Customer)stack.Pop();
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:
Stack stack = new Stack();
stack.Push(3);
int i = (int)stack.Pop();
Such boxing and unboxing operations add performance overhead since they involve dynamic memory allocations and run-time type checks.
A further issue with the Stack class is that it is not possible to enforce the kind of data placed on a stack. Indeed, a Customer instance can be pushed on a stack and then accidentally cast it to the wrong type after it is retrieved:
Stack stack = new Stack();
stack.Push(new Customer());
string s = (string)stack.Pop();
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.
With generics those problems are all solved. HOW ??
public class Stack<T>
{
T[] items;
int count;
public void Push(T item) {...}
public T Pop() {...}
}
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:
Stack<int> stack = new Stack<int>();
stack.Push(3);
int x = stack.Pop();
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.
Generics provide strong typing, meaning for example that it is an error to push an int onto a stack of Customer objects. Just as a Stack<int> is restricted to operate only on int values, so is Stack<Customer> restricted to Customer objects, and the compiler will report errors on the last two lines of the following example:
Stack<Customer> stack = new Stack<Customer>();
stack.Push(new Customer());
Customer c = stack.Pop();
stack.Push(3); // Type mismatch error
int x = stack.Pop(); // Type mismatch error
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.
相关文章:
- · 关于Visual C#装箱与拆箱的研究
- · C#中调用消息对话框,并取得其返回值
- · 在C#中应用HOOK
- · C#字符串的使用笔记
- · 解读 C# 中的正则表达式
- · 用正则表达式解析C#文件(updated)
- · C#中using关键字的使用
- · Vsiaul C#如何读取注册信息
- · 使用WMI列出Windows中某个目录的用户权限(C#)
- · Visual C#中的多线程编程
- · 在Windows下让不同用户使用不同的分辨率(C# 2005)
- · Regular Expression 正则表达式-1 (C#)
- · 一步一步用Visual C#创建Web服务
- · C# 2.0 匿名方法与 Windows Forms 异步调用
- · 由C#风潮想起的-给初学编程者的忠告
- · c#中结构与类的区别
- · C#初学乍练-文本替换工具命令行版
- · 点注《C# Coding Standard》Charpter One
- · 使用 C# 和 C++.NET 开发的 .NET 应用程序实例列表
- · 软件架构(C#)
- · IDesign C#编码规范(总结)
- · IDesign C#编码规范(之十一)
- · IDesign C#编码规范(之十)
- · IDesign C#编码规范(之九)
- · IDesign C#编码规范(之八)
- · IDesign C#编码规范(之七)
- · IDesign C#编码规范(之六)
- · 在C#中如何在客户端接收信件
- · 用C#程序实现键盘和鼠标的模拟
- · 我的第一个C#程序!
- · 在C#中如何发送Email
- · 关于C#中虚方法重载的说明
- · OwnerDraw in C#
- · VB中類模塊實現與C++中類實現的比較(1)
- · C#中虛函數,抽象,接口的簡單説明
- · C#软件启动设计
- · 在C#中实现打印功能(C#中PrintDialog,PrintDocument的使用)
- · C#下Socket对象的BeginReceive方法
