代码改变世界

C#中的default

2008-11-06 09:33  kenty06  阅读(295)  评论(0)    收藏  举报
default不仅用在 switch语句中
还可以为变量赋初始值

eg:     int i=default(int);  //默认值为 0

当然,在泛型中也有它的身影。

在泛型类和泛型方法中,在预先未知以下情况时,如何将默认值分配给参数化类型 T:

    

public class GenericList<T>
{
 
    public T GetT()
    {
        T temp = default(T);
        return temp;
    }
}

  GenericList<int> list1 = new GenericList<int>();
       
        Response.Write(list1.GetT().ToString());