C#之泛型

泛型

概念

泛型:广泛的类型,一般把方法传入类型的作用,关键字<T>

方法定义的时候可以把参数或者返回值写成泛型,调用的时候传递实参来决定参数的类型

用通俗来讲假设想要方法的参数可以接收不同的类型,可以传递整数也可以产地其他类型,可以把参数类型定义成泛型的

泛型的场景

List<int>list=new List <int>();<int>就是泛型

Dictionary<int,string> dic =new Dictionary<int,string>();

代码

cs 复制代码
//语法:修饰符 静态/非静态 方法返回值 方法名<T>(T a,T b)
//凡是所有T的地方类型保持一致
public static void Text1<T>(T a){ }

public static void Text1<T>(T a,T b)
{
Console.WriteLine(a.ToString()+b.ToString());
}
main函数:
{
 Text1<int>(10);//<T>决定参数类型 可以省略
 Text1("zs");
 Text1<int[]>(new int[] { 1, 2, 3, 4 });
 Text1<DateTime>(DateTime.Now);
cs 复制代码
//传递两个参数,根据传入的参数类型,返回一个泛型数组
public static T[] Text1<T>(T a,int count )// a传递的元素,count是数组的个数
{
T[]t1=new T[count];
for(int i=0;i<count;i++)
{
ti[i]=a//把a添加到t1数组里面,添加count次
}
return t1;
}

mian
{
 Text1<int>(10, 20);//两个泛型函数
 int[] ints = Text1<int>(10, 20);
 Array.ForEach(ints, i => { Console.WriteLine(i + "---"); });//10打印20次
}

泛型类

概念

泛型方法:只需要在方法后面加<T,T1,T2>,目的是为了确定参数类型和返回值类型,也可以把参数类型和返回值类型定义成普通类型

泛型类:定义类的时候在类名后面添加<T>,目的把类型传入类当中

泛型接口:在接口后面添加泛型,目的把类型传入接口当中

代码

cs 复制代码
//反省字母可以写任意的字母
public class People<TText1,TText2>
{
 public string Name{get;set;}
 public TText1 A1 { get; set; }//这个属性的类型和TText1类型保持一致
 public TText2 A2 { get; set; }//这个属性的类型和TText2类型保持一致
 public void F1<T>(T c, TText1 a, TText2 b)
 {
        dynamic sum = (dynamic)a + (dynamic)b + (dynamic)c;
        Console.WriteLine(sum);
 }
}

main
{
  People<int,float>p1=new People<int,float>();//传入int 和float类型
  p1.F1<float>(10.1f, 12, 10.1f);

  People<string ,string >p2=new People<string ,string>();
  p2.F1<string >("100", "oqo", "aqa");
}
cs 复制代码
interface IPeople<T>//接口
{
 int Age {  get; set; }
 T Name { get; set; }
 void F1<T1>(T a, T1 b);//T1是为了不和上面T重复
}
class Student:IPeople<float>
{
    public int Age { get; set; }
    public float Name { get; set; }
    public void F1<T1>(float a, T1 b)
    {
        Console.WriteLine(a + b.ToString());
    }
}
mian
{
   Student s1=new Student();
   s1.Age = 1;
   s1.Name = 10.1f;
   s1.F1<int>(10, 20);//1020
}

泛型约束

概念

泛型约束:泛型本身是没有限制类型的,但是可以通过where对泛型进行限制范围

cs 复制代码
//泛型本身是没有类型限制的
public static void Text1<T>(T a){}
//调用Text1方法
Text1<int>(10);
Text1("hello");

 //where T : struct限制T只能是值类型
 public static void Text2<T>(T a,T b)where T : struct { }
//调用Text2方法
Text2(10, 10);
// Text2<DateTime.Now,int>(DateTime.Now,10);报错

 //where T : class限制T只能是引用类型
 public static void Text3<T>(T a, T b) where T : class { }

// where T : new()限制T有一个 不带参数 的构造函数
public static void Text4<T>(T a, T b) where T : new() { }
//调用Text4方法
Text4(new People(), new People());

 //where T : IPeople必须是实现接口的类型、或者是实现接口的派生类,本例可以是Student、还可以继承于Student的子类SmallStudent
 public static void Text5<T>(T a, T b) where T : IPeople{ }
 //调用Text5方法
 Text5(new Student (), new SmallStudent());

 //where T : U 限制传入的类型,要么是T和U是同一的类型、要么T是U的子类
 public static void Text6<T,U>(T a, U b) where T : U { }
//调用Text6方法
Text6(new People(),new People());
Text6(new SmallStudent(), new Student());
//Text6(new Student(),new SmallStudent()); 报错

   interface IPeople { }
   public  class Student:IPeople { }//Student实现接口
   public class SmallStudent:Student { }//SmallStudent继承了Student
   public class People { }
相关推荐
bugcome_com2 小时前
深入浅出 C# 索引器:不止于数字索引的灵活利器
c#
工程师0072 小时前
C# 调用 Win32 API
开发语言·c#·api·win32
双河子思2 小时前
C# 语言编程经验
开发语言·c#
FuckPatience2 小时前
C# 把halcon中的任意图形HXLD在WPF中绘制出来
开发语言·c#
先生沉默先2 小时前
c#Socket学习,使用Socket创建一个在线聊天,数据模型(2)
服务器·学习·c#
王家视频教程图书馆2 小时前
C#使用 tcp socket控制台程序和unity客户端通信
开发语言·tcp/ip·c#
MyBFuture2 小时前
C#抽象类与重载重写实战
开发语言·c#·visual studio
唐青枫3 小时前
C#.NET AsyncLock 完全解析:async/await 下的并发控制方案
c#·.net
六bring个六14 小时前
文件压缩处理(一)
开发语言·c#