C#Where关键字泛型类型约束介绍,new()的作用

where关键词一个最重要的用法就是在泛型的声明、定义中做出约束。 约束又分为接口约束、基类约束、构造函数约束、函数方法的约束,接下来我们通过代码来一一介绍这些约束

where 泛型接口约束

泛型接口约束那就是参数必须实现相应的接口才可以,下面用代码展示一下:

//接口定义
public interface ICarFactory
{

    string Name
    {
        get;
    }

    string Type
    {
        get;
    }
}
//实现接口的类
public class Car : ICarFactory
{

    private string name;
    public string Name
    {
        get
        {
            return name;
        }

    }

    private string type;
    public string Type
    {
        get
        {
            return type;
        }
    }

    public Car():this("jiaoche", "AD")
    {
        
    }

    public Car(string name = "1", string type = "2")
    {
        this.name = name;
        this.type = type;
    }

}
//泛型约束,Where 后面的 T 一定要是实现了接口 ICarFactory 的类或者是接口 ICarFactory
using UnityEngine;

public class InterfaceTest<T> where T : ICarFactory
{

    public InterfaceTest()
    {
        Debug.Log("=====构造方法被调用=====");
    }

}

 

using UnityEngine;
//测试接口泛型的类
public class CarFactoryTest : MonoBehaviour
{
    // Start is called before the first frame update
    void Start()
    {
        //成功,Car实现了ICarFactory接口
        InterfaceTest<Car> mc1 = new InterfaceTest<Car>();
        Debug.Log("11111111=="+mc1.GetType());

        InterfaceTest<ICarFactory> mc2 = new InterfaceTest<ICarFactory>();
        Debug.Log("22222222=="+mc2.GetType());

        //构造失败,string没有实现ICarFactory接口,编译器提示错误
        // InterfaceTest<String> mc3 = new InterfaceTest<String>();
    }

    // Update is called once per frame
    void Update()
    {
        
    }
}

测试结果打印如下图:

 where 泛型类约束以及构造约束一起讲解


using UnityEngine;

public class DriveCar : Car
{

    public DriveCar()
    {
        Debug.Log("对应 new 无参数的构造函数,否则会报错");
    }


    public DriveCar(int a,int b) {
        Debug.Log("DriveCar 构造函数被调用");
    }
}

 

using UnityEngine;
//T 约束是类 DriveCar
public class DriveCarTest<T> where T : DriveCar, new()
{
    public DriveCarTest()
    {
        Debug.Log("=====DriveCarTest 类约束构造函数被调用=====");
    }
}

 

        //泛型类约束测试
        DriveCarTest<DriveCar> car1 = new DriveCarTest<DriveCar>();
        Debug.Log("333333333==" + car1.GetType());
        //错误的泛型参数
       // DriveCarTest<ICarFactory> errCar = new DriveCarTest<ICarFactory>();

 测试类约束结果

总结

where T : struct 这表明T必须是一个值类型,像是int,decimal这样的
where T : class 这表明T必须是一个引用类型,像是自定义的类、接口、委托等
where T : new() 这表明T必须有无参构造函数,且如果有多个where约束,new()放在最后面
where T : [基类] 这表明T必须是base class类获其派生类
where T : [接口] 这表明T必须实现了相应的接口

如果还需要拓展可继续查看如下文档:

https://docs.microsoft.com/zh-cn/dotnet/csharp/language-reference/keywords/where-generic-type-constraint

https://blog.csdn.net/ldy597321444/article/details/52956226?utm_medium=distribute.pc_relevant.none-task-blog-OPENSEARCH-1.nonecase&depth_1-utm_source=distribute.pc_relevant.none-task-blog-OPENSEARCH-1.nonecase

  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值