/// <summary>
/// 泛型类和泛型方法
/// </summary>
public class A
{
public T Fn<T>() where T : List<string>, new() //类型T约束为只能通过new关键字,如果取消new(),则方法里面不能用T t=new T() 如
果换成where T:global::System.Data的话 那么说明泛型T只能是System.Data命名空间下的类
{
T t = new T();
t.Add("11");
t.Add("22");
t.Add("33");
t.Add("44");
t.Add("55");
return t;
}
}
public class B
{
public void Fn()
{
A a = new A();
List<string> list = a.Fn<List<string>>();
for (int i = 0; i < list.Count; i++)
Console.WriteLine("从返回的泛型方法获取的第{0}个值为:" + list[i], i + 1);
}
}
/// 泛型类和泛型方法
/// </summary>
public class A
{
public T Fn<T>() where T : List<string>, new() //类型T约束为只能通过new关键字,如果取消new(),则方法里面不能用T t=new T() 如
果换成where T:global::System.Data的话 那么说明泛型T只能是System.Data命名空间下的类
{
T t = new T();
t.Add("11");
t.Add("22");
t.Add("33");
t.Add("44");
t.Add("55");
return t;
}
}
public class B
{
public void Fn()
{
A a = new A();
List<string> list = a.Fn<List<string>>();
for (int i = 0; i < list.Count; i++)
Console.WriteLine("从返回的泛型方法获取的第{0}个值为:" + list[i], i + 1);
}
}