[英]When should I use <> (or <,>)?
At the moment I am struggling a bit with generic typeing. 目前,我在通用类型方面有些挣扎。
While reading about it I sometimes encounter ISomeType<>
. 在阅读它时,有时会遇到
ISomeType<>
。
Eg: 例如:
Type generic = typeof(Dictionary<,>);
https://msdn.microsoft.com/en-us/library/system.type.makegenerictype%28v=vs.110%29.aspx https://msdn.microsoft.com/en-us/library/system.type.makegenerictype%28v=vs.110%29.aspx
I can't really find any documentation on wat the empty <>
means. 我真的找不到关于空
<>
wat文档。
So: When should I use <>
or <,>
? 所以:什么时候应该使用
<>
或<,>
?
update : 更新 :
As @dcastro stated, it is called open generics and more info can be found here: Generics -Open and closed constructed Types 如@dcastro所述,它称为开放泛型,并且可以在此处找到更多信息: 泛型-开放和封闭构造类型
update 2 更新2
As for the closure argument, this question is about the meaning of the syntax. 至于闭包参数,这个问题与语法的含义有关。
You'll use <,> when you're creating a generic type that accepts multiple type parameters, and <> when your type accepts a single parameter. 创建将接受多个类型参数的泛型类型时,将使用<,>;当您的类型接受单个参数时,将使用<>。
For example, Dictionaries are instantiated with a key/value pair where you must define the type of both the key and value, so you pass a type argument for both key and value. 例如,字典是用键/值对实例化的,您必须在其中定义键和值的类型,因此您必须为键和值传递类型实参。
Dictionary<type1,type2> myDictionary = new Dictionary<type1,type2>();
On the other hand, Lists accepts only a single type parameter. 另一方面,列表仅接受单个类型参数。
List<type> myList = new List<type>();
An assembly can define, within the same namespace, multiple types with the same name, as long as the number of type arguments (generic parameters) differs for each type. 程序集可以在同一名称空间中定义多个具有相同名称的类型,只要每种类型的类型参数(通用参数)的数量不同即可。 Try for example:
尝试例如:
var strA = typeof(Action).ToString(); // "System.Action"
var strB = typeof(Action<>).ToString(); // "System.Action`1[T]"
var strC = typeof(Action<,>).ToString(); // "System.Action`2[T1,T2]"
var strD = typeof(Action<,,>).ToString(); // "System.Action`3[T1,T2,T3]"
In C#, one can only use empty <...>
, ie avoid specifying what the type parameters are, within the typeof(...)
keyword. 在C#中,只能使用空的
<...>
,即避免在typeof(...)
关键字内指定类型参数是什么。
Another example: typeof(Dictionary<,>).ToString()
gives "System.Collections.Generic.Dictionary`2[TKey,TValue]"
. 另一个示例:
typeof(Dictionary<,>).ToString()
给出"System.Collections.Generic.Dictionary`2[TKey,TValue]"
。 As you can see, in .NET (the CLI) the number of generic parameters is given by appending a backtick followed by digit(s). 如您所见,在.NET(CLI)中,通用参数的数目是通过在反引号后附加数字来给出的。
Examples of use: 使用示例:
static bool IsDictionary(object obj)
{
if (obj == null)
return false;
var t = obj.GetType();
return t.IsGenericType && t.GetGenericTypeDefinition() == typeof(Dictionary<,>);
}
static object GetDictionary(object a, object b)
{
var t = typeof(Dictionary<,>).MakeGenericType(a.GetType(), b.GetType());
return Activator.CreateInstance(t);
}
But if possible, avoid reflection and use compile-time types, for example (not quite equivalent to the above): 但是,如果可能,请避免反射,并使用编译时类型,例如(与上面的方法不太等效):
static Dictionary<TA, TB> GetDictionary<TA, TB>(TA a, TB b)
{
return new Dictionary<TA, TB>();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.