包括时的命名空间,程序集和继承层次结构 [英] Namespace, assembly, and inheritance hierarchy when including

查看:88
本文介绍了包括时的命名空间,程序集和继承层次结构的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

通常在我的代码中包含名称空间或程序集时,我经常会遇到奇怪的情况,即名称空间是从另一个名称空间继承而来的,而父名称空间的类却不可用.例如,当使用List<>Dictionary<>时,我使用System.Collections.Generic命名空间.但是,如果我还想使用IEnumerator,则还必须包括System.Collections命名空间. System.Collections是子项,不是System.Collections的任何成员已经引用了吗?两个名称空间还共享相同的程序集mscorlib.dll.

Often when including namespaces or assemblies into my code, I often run into strange cases where a namespace is inherited from another, yet classes from the parent namespace are not available. For example, when using List<> or Dictionary<>, I use the System.Collections.Generic namespace. However, if I also want to use an IEnumerator, I also have to include the System.Collections namespace. Shouldn't System.Collections already be referenced by any member of System.Collections.Generic, as it is a child? Both namespaces also share the same assembly, mscorlib.dll.

为什么子级不包含父级名称空间?

Why is the parent namespace not included when the child is?

推荐答案

System.Collections.Generic的任何成员都应该已经引用它了吗?

Shouldn't System.Collections already be referenced by any member of System.Collections.Generic, as it is a child?

不.名称空间之间没有继承. using指令 only 仅从该命名空间导入类型-它不从具有给定命名空间的命名空间 starting 或给定命名空间名称中包含的命名空间导入类型.

No. There's no inheritance between namespaces. A using directive only imports types from that namespace - it doesn't import types from namespaces starting with the given namespace, or namespaces included within the given namespace's name.

例如,在之后:

using System.Collections;

...不允许您将List<T>用作System.Collections.Generic.List<T>的简单名称,也不允许您将Guid用作System.Guid的简单名称.

... that doesn't let you use List<T> as a simple name for System.Collections.Generic.List<T>, nor does it let you use Guid as a simple name for System.Guid.

的例外是,如果您在X.Y.Z的名称空间声明中编写代码,则会隐式导入名称空间X.YX(以及X.Y.Z , 当然). (在该示例中,X.Y封闭的名称空间(如果X.Y.Z,但不是继承关系.)

The only exception to this is that if you're writing code within a namespace declaration of X.Y.Z, that implicitly imports namespaces X.Y and X (as well as X.Y.Z, of course). (In that example, X.Y is the enclosing namespace if X.Y.Z, but it's not an inheritance relationship.)

这篇关于包括时的命名空间,程序集和继承层次结构的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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