问题描述
最近我注意到泛型构造类型可以打开和关闭。但是我不明白它们的真正含义。您能举一个简单的例子吗?
Recently I noticed that generic constructed types can be open and closed. But I do not understand what they actually mean. Can you give a simple example?
推荐答案
在实践中,该术语并没有多大意义-我不记得最后一个
In practice the terminology doesn't really matter much - I can't remember the last time I had to worry about it except when trying to write about it.
- unbound 类型未指定类型参数
- 构造的 类型至少指定了一个类型参数
- 一个类型参数是开放类型
- 打开元素类型的数组类型是开放类型
- 一个 open 构造类型具有至少一个类型参数,这是一个开放类型
- closed 类型是任何类型
- An unbound type has no type arguments specified
- A constructed type has at least one type argument specified
- A type parameter is an open type
- An array type where the element type is open is an open type
- An open constructed type has at least one type argument which is an open type
- A closed type is any type which isn't open
(嵌套类型还有其他规则。有关详细信息,请参阅C#3.0规范第4.4节。)
(There are further rules for nested types. Consult the C# 3.0 spec section 4.4 for gory details.)
作为开放构造类型的示例,请考虑:
As an example of an open constructed type, consider:
public class NameDictionary<T> : Dictionary<string, T>
typeof(NameDictionary<>))的基类
是:
- 之所以构造是因为它指定了类型参数
- 因为第二个类型参数而打开(
T
)是一种开放类型
- Constructed because it specifies type arguments
- Open because the second type argument (
T
) is an open type
有一个非常有用的小桌子。
The MSDN docs for Type.IsGenericType
have quite a useful little table.
只需重申一下,在日常使用中这几乎完全不重要。
Just to reiterate, this is almost entirely unimportant in day to day use.
通常,我赞成知道正确的术语-特别是对于通过引用之类的术语-但在这种情况下,它的确确实很少出现。我想积极劝阻您不要担心:)
I'm generally in favour of knowing the correct terminology - particularly for things like "pass by reference" etc - but in this case it really, really doesn't come up very often. I would like to actively discourage you from worrying about it :)
这篇关于泛型开放和封闭构造类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!