目录
概念
让泛型的类型有一定的限制
关键字:where
泛型约束一共有6种
1.值类型 where 泛型字母:struct
2.引用类型 where 泛型字母:class
3.存在无参公共构造函数 where 泛型字母:new()
4.某个类本身或者其派生类 where 泛型字母:类名
5.某个接口的派生类型 where 泛型字母:接口名
6.另一个泛型类型本身或者派生类型 where 泛型字母:另一个泛型字母
where 泛型字母:(约束的类型)
1.值类型约束
class Test1<T> where T:struct
{
public T value;
public void TestFun<K>(K v) where K:struct
{
}
}
main
{
Test1<int> t1 = new Test1<int>();
t1.TestFun<float>(1.3f);
}
2.引用类型约束
class Test2<T> where T:class
{
public T value;
public void TestFun<K>(K k) where K:class
{
}
}
main
{
Test2<Random> t2 = new Test2<Random>();
t2.value = new Random();
t2.TestFun<object>(new object());
}
3.公共无参构造约束
class Test3<T> where T:new()
{
public T value;
public void TestFun<K>(K k) where K : new()
{
}
}
class Test1
{
public Test1(){ }
}
class Test2
{
public Test2(int a){ }
}
main
{
Test3<Test1> = new Test();
// Test3<Test2>=new Test(); Test2的构造函数有参数不符合公共无参的标准
}
4.类约束
class Test4<T> where T : Test1
{
public T value;
public void TestFun<K>(K k) where K : Test1
{
}
}
class Test3:Test1
{
}
main
{
可以是Test1和他的派生类
Test4<Test3> t4 = new Test4<Test3>();
}
5.接口约束
interface IFly
{
}
interface IMove:IFly
{
}
class Test4:IFly
{
}
class Test5<T> where T : IFly
{
public T value;
public void TestFun<K>(K k) where K : IFly
{
}
}
main
{
Test5<IMove> t5 = new Test5<IMove>();
Test5<IFly> t6 = new Test5<IFly>();
t6.value = new Test4(); //Test4继承了IFly接口
}
6.另一个泛型约束
class Test6<T,U> where T : U //T的类型是Test4 U的类型是IFly 这里的T可以是IFly本身或者是其
{ //派生类型,Test4继承了IFly接口
public T value;
public void TestFun<K,V>(K k) where K : V
{
}
}
main
{
Test6<Test4, IFly> t6 = new Test6<Test4, IFly>();
}
约束的组合使用
class Test7<T> where T: class,new()
{
}
多个泛型有约束
class Test8<T,K> where T:class,new() where K:struct
{
}
泛型单例
class SingleForm<K> where K:new()
{
private static SingleForm<K> singleForm;
protected SingleForm() { }
public static SingleForm<K> Single
{
get { return singleForm=new SingleForm<K>(); }
}
}
class Manager : SingleForm<Manager>
{
}
泛型ArrayList
class ArrayList<T>
{
private T [] array;
public int Capacity
{
get { return array.Length; }
}
private int length;
public int Length
{
get { return length; }
}
public ArrayList()
{
array = new T [16];
length = 0;
}
public T this[int index]
{
get
{
if (index >= length)
{
Console.WriteLine("索引不合法");
return default(T);
}
return array[index];
}
set
{
if (index >= length)
{
Console.WriteLine("索引不合法");
return; //表示不执行下面的代码
}
array[index] = value;
}
}
public void Add(T a)
{
if (length == Capacity)
{
T [] temp = new T [Capacity *2];
for (int i = 0; i < Capacity ; i++)
{
temp [i] = array [i];
}
array = temp;
}
array[length] = a;
length++;
}
public void RemoveAt(int index)
{
if (index >= length)
{
Console.WriteLine("索引不合法");
return;
}
for (int i = index ; i < length; i++)
{
array[index ]=array[index+1];
}
length--;
array[length] = default(T);
}
}
class Program
{
public static void Main()
{
ArrayList<int> arrayList = new ArrayList<int>();
Console.WriteLine(arrayList.Capacity);
Console.WriteLine(arrayList.Length);
arrayList.Add(1);
arrayList.Add(2);
arrayList.Add(3);
arrayList[0] = 100;
arrayList .RemoveAt(2);
Console.WriteLine(arrayList.Capacity);
Console.WriteLine(arrayList.Length);
Console.WriteLine (arrayList[0]);
}
}