T

class ClaseName<T> where T : class, new()

 

where T : class
==
T类型必须是引用类型(数组、类、接口、委托)
new()  
==
T类型必须有默认构造函数

where T:new()

public class Assemble<T> where T:new
这句话的意思是声明了一个叫做Assemble<T>的泛型类,然后T的限制为必须要有一个无参构造器。


public class Assemble<T> where T : new()
    {
        
public string SayHello<T>(T t)
        {
            T ass 
= new T();
            
return ass.ToString();
        }
    }

    
public class legal
    {
        
public legal()
        { }
    }

    
public class notlegal
    {
        
public notlegal(string someting)
        { }
    }

当声明Assemble<legal> ass=new Assemble<legal>();时相当于声明了一个Assemble<T>,其中第一个代码端中的T全部替换为legal。当声明Assemble<notlegal> ass=new Assemble<notlegal>();时,由于Assemble<T>中T要求必须具有无参构造器,所以,这句话会报错。

posted @ 2013-11-04 22:24  .L  阅读(229)  评论(0编辑  收藏  举报