开发手册 欢迎您!
软件开发者资料库

.NET Core(C#)泛型<T>中约束的使用及示例代码

泛型使用有时需要对T进制约束,上下文关键字where用以实现约束,限定构类函数及类型等,本文主要介绍使用泛型约束及示例代码。

1、常用五类约束

where T: struct :类型参数必须为值类型。

where T : class :类型参数必须为引用类型。

where T : new() :类型参数必须有一个公有、无参的构造函数。当于其它约束联合使用时,new()约束必须放在最后。

where T : :类型参数必须是指定的基类型或是派生自指定的基类型。

where T : :类型参数必须是指定的接口或是指定接口的实现。可以指定多个接口约束。接口约束也可以是泛型的。

注意:类型参数的约束,增加了可调用的操作和方法的数量。这些操作和方法受约束类型及其派生层次中的类型的支持。因此,设计泛型类或方法时,如果对泛型成员执行任何赋值以外的操作,或者是调用System.Object中所没有的方法,就需要在类型参数上使用约束

2、约束使用示例代码

using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;namespace MyGeneric{    public class Constraint    {        ///         /// 泛型:不同的参数类型都能进来;任何类型都能过来,你知道我是谁?        /// 没有约束,也就没有自由        /// 泛型约束--基类约束(不能是sealed):        /// 1 可以使用基类的一切属性方法---权利        /// 2  强制保证T一定是People或者People的子类---义务        ///         ///         ///         public static void Show(T tParameter)            where T : People, ISports, IWork, new()        {            //Console.WriteLine("This is {0},parameter={1},type={2}",            //    typeof(GenericMethod), tParameter.GetType().Name, tParameter.ToString());            Console.WriteLine($"{tParameter.Id}_{tParameter.Name}");            tParameter.Hi();            //tParameter.Majiang();            tParameter.Pingpang();            tParameter.Work();        }        public static void ShowBase(People tParameter)//约束可以叠加  更灵活        {            Console.WriteLine($"{tParameter.Id}_{tParameter.Name}");            tParameter.Hi();        }        public static T Get(T t)            //where T : ISports//接口约束            //where T : class//引用类型约束            //where T : struct//值类型约束            where T : new()//无参数构造函数约束        {            //t.Pingpang();            //T tNew = null;            //T tNew = default(T);//会根据T的不同  赋予默认值            T tNew = new T();            return t;        }    }}

3、无类型约束

当约束是一个泛型类型参数时,它就叫无类型约束(Naked type constraints)。当一个有类型参数成员方法,要把它的参数约束为其所在类的类型参数时,无类型约束很有用。如下例所示: 

class List{      //...    void Add(List items) where U:T {…}}




2009-2024 Copyright Wonhero.Com All Rights Reserved
深圳幻海软件技术有限公司 版权所有