.Net C# 泛型 类型参数约束

在开发过程中,我们定义的泛型类或泛型方法默认都是无约束的,可以传入任何类型的参数,在有的时候方法处理逻辑并不一定能够处理某种类型的参数,就会导致程序运行出错。这个时候就需要我们给泛型类或泛型方法指定某些约束,以便于程序安全稳定的运行。

参考代码:

将方法约束为引用类型参数:

        public static string GetT1<T>(T t) where T : class
        {
            return typeof(T) + "|+|" + t;
        }

将方法约束为值类型参数:

        public static string GetT2<T>(T t) where T : struct
        {
            return typeof(T) + "|+|" + t;
        }

将方法约束为必须带有无参构造函数的类型:

        public static string GetT3<T>(T t) where T : new()
        {
            return typeof(T) + "|+|" + t;
        }

将方法约束为带有公共无参构造函数的引用类型

        public static string GetT4<T>(T t) where T : class, new()
        {
            return typeof(T) + "|+|" + t;
        }

将方法约束为
        /// 第一个参数:只能是带有公共无参构造函数的引用类型
        /// 第二个参数:只能是值类型

        public static string GetT5<T, S>(T t, S s)
            where T : class, new()
            where S : struct
        {
            return typeof(T) + "|+|" + t + "|+|" + typeof(S) + "|+|" + s;
        }

在实际应用中不止上面展示的这几种约束,还有很多约束需要在我们使用时去应用。而且约束是既可以加在方法后面也可以加在类名后面的。

    class GenericDemo1<T, S, U>
        where T : class
        where S : class
        where U : struct
    {

    }

下面列出了官方文档提供的约束说明:

约束描述
where T : struct类型参数必须是不可为 null 的值类型。 有关可为 null 的值类型的信息,请参阅可为 null 的值类型。 由于所有值类型都具有可访问的无参数构造函数,因此 struct 约束表示 new() 约束,并且不能与 new() 约束结合使用。 struct 约束也不能与 unmanaged 约束结合使用。
where T : class类型参数必须是引用类型。 此约束还应用于任何类、接口、委托或数组类型。 在 C#8.0 或更高版本中的可为 null 上下文中,T 必须是不可为 null 的引用类型。
where T : class?类型参数必须是可为 null 或不可为 null 的引用类型。 此约束还应用于任何类、接口、委托或数组类型。
where T : notnull类型参数必须是不可为 null 的类型。 参数可以是 C# 8.0 或更高版本中的不可为 null 的引用类型,也可以是不可为 null 的值类型。
where T : default重写方法或提供显式接口实现时,如果需要指定不受约束的类型参数,此约束可解决歧义。 default 约束表示基方法,但不包含 class  struct 约束。 有关详细信息,请参阅default约束规范建议。
where T : unmanaged类型参数必须是不可为 null 的非托管类型。 unmanaged 约束表示 struct 约束,且不能与 struct 约束或 new() 约束结合使用。
where T : new()类型参数必须具有公共无参数构造函数。 与其他约束一起使用时,new() 约束必须最后指定。 new() 约束不能与 struct  unmanaged 约束结合使用。
where T : <base class name>类型参数必须是指定的基类或派生自指定的基类。 在 C# 8.0 及更高版本中的可为 null 上下文中,T 必须是从指定基类派生的不可为 null 的引用类型。
where T : <base class name>?类型参数必须是指定的基类或派生自指定的基类。  C# 8.0 及更高版本中的可为 null 上下文中,T 可以是从指定基类派生的可为 null 或不可为 null 的类型。
where T : <interface name>类型参数必须是指定的接口或实现指定的接口。 可指定多个接口约束。 约束接口也可以是泛型。  C# 8.0 及更高版本中的可为 null 上下文中,T 必须是实现指定接口的不可为 null 的类型。
where T : <interface name>?类型参数必须是指定的接口或实现指定的接口。 可指定多个接口约束。 约束接口也可以是泛型。  C# 8.0 中的可为 null 上下文中,T 可以是可为 null 的引用类型、不可为 null 的引用类型或值类型。 T 不能是可为 null 的值类型。
where T : U为 T 提供的类型参数必须是为 U 提供的参数或派生自为 U 提供的参数。 在可为 null 的上下文中,如果 U 是不可为 null 的引用类型,T 必须是不可为 null 的引用类型。 如果 U 是可为 null 的引用类型,则 T 可以是可为 null 的引用类型,也可以是不可为 null 的引用类型。

完整演示代码:

    class GenericDemo
    {
        public static void GenericMain()
        {
            Console.WriteLine(GetT1("1-1"));
            Console.WriteLine(GetT2(1));
            Console.WriteLine(GetT3(2.1));
            Console.WriteLine(GetT4(new StringBuilder("ss")));
            Console.WriteLine(GetT5(new StringBuilder("ss"), 6.666666));
        }
        /// <summary>
        /// 将方法约束为引用类型参数
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="t"></param>
        /// <returns></returns>
        public static string GetT1<T>(T t) where T : class
        {
            return typeof(T) + "|+|" + t;
        }
        /// <summary>
        /// 将方法约束为值类型参数
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="t"></param>
        /// <returns></returns>
        public static string GetT2<T>(T t) where T : struct
        {
            return typeof(T) + "|+|" + t;
        }
        /// <summary>
        /// 将方法约束为必须带有无参构造函数的类型
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="t"></param>
        /// <returns></returns>
        public static string GetT3<T>(T t) where T : new()
        {
            return typeof(T) + "|+|" + t;
        }
        /// <summary>
        /// 将方法约束为带有公共无参构造函数的引用类型
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="t"></param>
        /// <returns></returns>
        public static string GetT4<T>(T t) where T : class, new()
        {
            return typeof(T) + "|+|" + t;
        }
        /// <summary>
        /// 将方法约束为
        /// 第一个参数:只能是带有公共无参构造函数的引用类型
        /// 第二个参数:只能是值类型
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <typeparam name="S"></typeparam>
        /// <param name="t"></param>
        /// <param name="s"></param>
        /// <returns></returns>
        public static string GetT5<T, S>(T t, S s)
            where T : class, new()
            where S : struct
        {
            return typeof(T) + "|+|" + t + "|+|" + typeof(S) + "|+|" + s;
        }
    }

    class GenericDemo1<T, S, U>
        where T : class
        where S : class
        where U : struct
    {

    }

输出结果如下:

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

一个堆栈

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值