加入收藏 | 设为首页 | 会员中心 | 我要投稿 北几岛 (https://www.beijidao.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

C# 泛型约束 xxx Where T:约束(二)

发布时间:2021-05-21 07:00:14 所属栏目:大数据 来源: https://www.jb51.cc
导读:? 泛型是什么? 通过上篇的实例? C# 泛型约束 xxxT Where T:约束(一),我们对泛型有一定的认识。 所谓泛型,即通过参数化类型来实现在同一份代码上操作多种数据类型,泛型编程是一种编程范式,它利用“参数化类型”将类型抽象化,从而实现更为灵活的复用。
约束 说明 T:struct 类型参数必须是值类型。可以指定除 Nullable 以外的任何值类型。 T:class 类型参数必须是引用类型,包括任何类、接口、委托或数组类型。 T:new () 类型参数必须具有无参数的公共构造函数。当与其他约束一起使用时,new() 约束必须最后指定。 T:<基类名> 类型参数必须是指定的基类或派生自指定的基类。 T:<接口名称> 类型参数必须是指定的接口或实现指定的接口。可以指定多个接口约束。约束接口也可以是泛型的。 T:U 为 T 提供的类型参数必须是为 U 提供的参数或派生自为 U 提供的参数。这称为裸类型约束.

image

1. 派生约束

1.常见的

public class MyClass5<T> where T :IComparable { }

2.约束放在类的实际派生之后

public class B { }

public class MyClass6<T> : B where T : IComparable { }

3.可以继承一个基类和多个接口,且基类在接口前面

public class B { }

public class MyClass7<T> where T : B,IComparable,ICloneable { }

2. 构造函数约束

1.常见的

public class MyClass8<T> where T :? new() { }

2.约束组合

可以将构造函数约束和派生约束组合起来,前提是构造函数约束出现在约束列表的最后

public class MyClass8<T> where T : IComparable,new() { }

3. 值约束

1.常见的

public class MyClass9<T> where T : struct { }

2. 接口约束同时使用

与接口约束同时使用,在最前面(不能与基类约束,构造函数约束一起使用)

public class MyClass11<T> where T : struct,IComparable { }

4. 引用约束

1.常见的

public class MyClass10<T> where T : class { }

5. 多个泛型参数

public class MyClass12<T,U> where T : IComparable? where U : class { }

6. 继承和泛型

public class B<T>{ }

1.类型实参

1. 在从泛型基类派生时,可以提供类型实参,而不是基类泛型参数

??? public class SubClass11 : B<int>{ }

2.子类泛型作为基类泛型的指定类型

2.如果子类是泛型,而非具体的类型实参,则可以使用子类泛型参数作为泛型基类的指定类型

??? public class SubClass12<R> : B<R>{ }

3.子类重复基类的约束

3.在子类重复基类的约束(在使用子类泛型参数时,必须在子类级别重复在基类级别规定的任何约束)
??? public class B<T> where T : ISomeInterface { }
??? public class SubClass2<T> : B<T> where T : ISomeInterface { }

4.构造函数约束

public class B<T> where T : new()
{
    public T SomeMethod()
    {
        return  T();
    }
}
class SubClass3<T> : B<T> new() { }

?

7. 泛型方法

C#2.0泛型机制支持在"方法声名上包含类型参数",这就是泛型方法)

1.泛型类型/非泛型类型

泛型方法既可以包含在泛型类型中,又可以包含在非泛型类型中

class MyClass5 { void MyMethod<T>(T t){ } }

2.泛型方法的声明与调用

(T t) { } } App5 { void CallMethod() { MyClass5 myclass5 = MyClass5(); myclass5.MyMethod<int>(3); } }

3.泛型方法的重载

//第一组重载void MyMethod1<T>(T t,int i) { } void MyMethod1<U>(U u,1)">第二组重载void MyMethod2<T>( i) { } void MyMethod2(第三组重载,假设有两个泛型参数void MyMethod3<T>(T t) where T : A { } T : B { } 第四组重载class MyClass8<T,U> { T MyMothed(T a,U b) { return a; } T MyMothed(U a,T b) { b; } int MyMothed(int a,1)"> b) { return a + b; } }

4.泛型方法的覆写

MyBaseClass1 { virtualvoid MyMothed<T>(T t) () { } } MySubClass1 : MyBaseClass1 { 不能重复任何约束overridevoid MyMothed<T>(T t) { } } MyBaseClass2 { (T t){ } } MySubClass2 : MyBaseClass2 { 重新定义泛型参数T(T t){ } }

8. 虚拟方法

class BaseClass4<T>virtualdefault(T); } } class SubClass4 : BaseClass4<int> 使用实参继承的时候方法要使用实参的类型{ SomeMethod() { return0; } } class SubClass5<T> : BaseClass4<T> 使用泛型继承时,方法也是泛型override(T); } }

9. 泛型参数强制转换到Object或约束指定的类型

编译器只允许将泛型参数隐式强制转换到 Object 或约束指定的类型

class MyClass<T> T : BaseClass,ISomeInterface { SomeMethod(T t) { ISomeInterface obj1 = t; BaseClass obj2 = t; object obj3 = t; } } 变通方法:使用临时的 Object 变量,将泛型参数强制转换到其他任何类型class MyClass2<T> SomeMethod(T t) { object temp = t; BaseClass obj = (BaseClass)temp; } }

10. 泛型参数强制转换到其他任何接口

编译器允许您将泛型参数显式强制转换到其他任何接口,但不能将其转换到类

class MyClass1<T> (ISomeInterface)t; BaseClass obj2 = (BaseClass)t; 不能通过编译 } }

11. 泛型参数强制转换到其他任何类型

使用临时的 Object 变量,将泛型参数强制转换到其他任何类型

(BaseClass)temp; } }

12. 使用is和as运算符

class MyClass3<T>if (t is) { } is LinkedList<int>string str = t asstring; if (str != null) { } LinkedList<int> list = t as LinkedList<if (list != ) { } } }

参考资料

MSDN:http://msdn.microsoft.com/zh-cn/library/d5x73970.aspx

部分内容参考:http://www.cnblogs.com/andrew-blog/archive/2012/03/21/ListT_Where.html

(编辑:北几岛)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

?

泛型是什么?

通过上篇的实例? C# 泛型约束 xxx<T> Where T:约束(一),我们对泛型有一定的认识。

所谓泛型,即通过参数化类型来实现在同一份代码上操作多种数据类型,泛型编程是一种编程范式,它利用“参数化类型”将类型抽象化,从而实现更为灵活的复用。

在定义泛型类时,可以对代码能够在实例化类时用于类型参数的类型种类施加限制。如果代码尝试使用某个约束所不允许的类型来实例化类,则会产生编译时错误。这些限制称为约束。约束是使用 where 上下文关键字指定的。

五种类型的约束

下表列出了五种类型的约束:

    推荐文章
      热点阅读