什么是“其中T:某些值”意思? [英] What does "where T : somevalue" mean?

查看:185
本文介绍了什么是“其中T:某些值”意思?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

其中T:somevalue 是什么意思?我只是看到一些代码说:其中T:Attribute 。我认为这与泛型有关,但我不知道这是什么意思,或者它在做什么。



有没有人知道?

解决方案

这是一个 对类型参数的约束 ,这意味着给予通用类或方法的类型 T 必须继承类属性



例如:

 code> public class Foo< T> :
其中T:Attribute
{
public string GetTypeId(T attr){return attr.TypeId.ToString(); }
// ..
}

Foo< DescriptionAttribute>酒吧; // OK,DescriptionAttribute inherits Attribute
Foo< int>巴兹//编译器错误,int不继承属性

这很有用,因为它允许通用类与 T 的对象做事情,知道任何 T 也必须是 Attribute



在上面的例子中, GetTypeId 可以查询 TypeId TypeId 属性的属性,所以 attr $ c>,并且因为 attr T ,它必须是继承自属性



约束也可用于通用方法,效果相同:

  public static void GetTypeId< T>(T attr)其中T:属性
{
return attr.TypeId.ToString();
}

来自 MSDN


其中T:struct



type参数必须是值
类型。可以指定除Nullable
之外的任何值类型。



其中T:class



必须是引用
类型;这也适用于任何类,
接口,委托或数组类型。



其中T:new()



类型参数必须有一个public
无参数的构造函数。当使用
和其他约束时,
new()约束必须指定
last。



其中T:< base class name>



type参数必须是或从指定的基类派生
。 p>

其中T:< interface name>



类型参数必须是或实现
指定的接口。多个
接口约束可以指定
。约束接口
也可以是通用的。



其中T:U



为T提供的类型参数必须为
be或从为U提供的参数
派生。这被称为裸体
类型约束。



What does where T : somevalue mean? I just saw some code that said where T : Attribute. I think this has something to do with generics but I am not sure what this means or what it is doing.

Does anyone know?

解决方案

It is a constraint on a type parameter, meaning that the type T given to a generic class or method must inherit from the class Attribute

For example:

public class Foo<T> : 
   where T : Attribute
{
    public string GetTypeId(T attr) { return attr.TypeId.ToString(); }
 // ..
}

Foo<DescriptionAttribute> bar; // OK, DescriptionAttribute inherits Attribute
Foo<int> baz; // Compiler error, int does not inherit Attribute

This is useful, because it allows the generic class to do things with objects of type T with the knowledge that anything that is a T must also be an Attribute.

In the example above, it's okay for GetTypeId to query the TypeId of attr because TypeId is a property of an Attribute, and because attr is a T it must be a type that inherits from Attribute.

Constraints can also be used on generic methods, with the same effect:

public static void GetTypeId<T>(T attr) where T : Attribute
{
   return attr.TypeId.ToString();
}

There are other constraints you can place on a type; from MSDN:

where T: struct

The type argument must be a value type. Any value type except Nullable can be specified.

where T : class

The type argument must be a reference type; this applies also to any class, interface, delegate, or array type.

where T : new()

The type argument must have a public parameterless constructor. When used together with other constraints, the new() constraint must be specified last.

where T : <base class name>

The type argument must be or derive from the specified base class.

where T : <interface name>

The type argument must be or implement the specified interface. Multiple interface constraints can be specified. The constraining interface can also be generic.

where T : U

The type argument supplied for T must be or derive from the argument supplied for U. This is called a naked type constraint.

这篇关于什么是“其中T:某些值”意思?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆