当前位置: 首页 > news >正文

C# 泛型约束

目录

概念

1.值类型约束

2.引用类型约束

3.公共无参构造约束

4.类约束

5.接口约束

6.另一个泛型约束

约束的组合使用

多个泛型有约束


概念

让泛型的类型有一定的限制
    关键字: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]);}}


http://www.mrgr.cn/news/13314.html

相关文章:

  • webpack和vite分别是什么,优势
  • 学习区块链?看我就够了!
  • MySQL索引(三)
  • 重装后的电脑怎么分区?轻松优化存储空间
  • 【苍穹外卖】Day2 员工接口 分类接口
  • BERT模型
  • C++语法基础(一)
  • 无人机 PX4 飞控 | ROS应用层开发:指令(字符串)订阅功能
  • 新增页面保存后,跳转为详情页,同时关闭新增页。(即路由detail/1?type=1,变为detail/2/2?type=2id=2)
  • Go学习笔记(一)语法
  • GNU/Linux - RSYSLOG
  • 移动端+PC端源码,智慧城管执法系统,后端框架:springboot,移动端:uniapp
  • Git实战精粹
  • RSA加密解密算法认识及signln_resolve
  • 初识redis:Zset有序集合
  • fastjson序列化时过滤字段的方法
  • C++ DAY2
  • XSS-labs靶场通关攻略(16-20)
  • 力扣229题详解:求众数 II 的多种解法与模拟面试问答
  • day-42 分割字符频率相等的最少子字符串