打开,关闭,绑定和未绑定的通用类型 [英] Open, Closed, Bound and Unbound Generic types

查看:49
本文介绍了打开,关闭,绑定和未绑定的通用类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经阅读了许多有关此的文章,但是我仍然不确定我是否完全理解这些定义.

I've read a number of posts about this but I'm still not sure that I fully understand the definitions.

我认为这是不同术语的示例.我是在这里吗?还是我仍然不理解这些概念.谢谢

Here's what I think are examples of the different terms. Am I on the right track here, or do I still not understand the concepts. Thanks

Array<T TArray> - unbound and open.
Array<int> - bound and closed.
Array<Array<T TArray> - bound and open.
Array<Array<int>> - bound and closed.

推荐答案

未绑定意味着类似 typeof(Dictionary<,>)之类的东西.未绑定类型仅对反射很有趣,并且只能在 typeof()中使用,而不能在任何其他上下文中使用.所有未绑定类型都是封闭类型,不可能同时使用未绑定和打开"组合.

Unbound means something like typeof(Dictionary<,>). Unbound types are only interesting for Reflection and can only be used in typeof(), not in any other context. All unbounds types are closed types, the combination "unbound and open" is impossible.

假设T是当前类/方法的类型参数:

Assuming T is a type parameter of the current class/method:

Dictionary<,> - unbound and closed
Dictionary<string, int> - constructed and closed
Dictionary<int, T> - constructed and open
Dictionary<string, List<T>> - constructed and open
NonGenericClass - bound and closed

请注意,没有 List< Dictionary<,>> 这样的东西-未绑定的类型不能用作类型参数,只能直接在 typeof()中使用.类型是未绑定的或完全绑定的.而且,如果类型是未绑定的,那么就没有地方可以引用类型参数,因此"unbound and open"的组合是不可能的.

Note that there is no such thing as List<Dictionary<,>> - unbound types cannot be used as type arguments, only directly in typeof(). A type is either unbound, or completely bound. And if a type is unbound, there's no place where it could refer to a type parameter, so the combination "unbound and open" is impossible.

这篇关于打开,关闭,绑定和未绑定的通用类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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