如果导入父命名空间,为什么要导入子命名空间? [英] Why Import Child Namespace if Parent Namespace Imported?

查看:30
本文介绍了如果导入父命名空间,为什么要导入子命名空间?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么会有人有

Import System

以及

Import System.Web.UI.WebControls

如果 System 是所有命名空间的根,那么在后面的代码中使用 Import System 有什么危害.

If System is root of all namespaces what is the harm in just having Import System in the code behind.

推荐答案

如果 import System 确实导入了所有较低级别的命名空间,那么几乎没有任何理由导入.

If import System did import all of the lower-level namespaces, then there would be almost no reason to have an import at all.

但我们确实有命名空间系统的原因是为了避免名称冲突,其中不同的包包含相同名称的类.

But the reason we do have a namespace system is to avoid name collisions where different packages contain classes of the same names.

例如,可能有两个不同的命名空间,(组成它们)System.SecureSystem.Insecure.两者都可以使用完全相同的名称来处理 http 的类,但是一个可以使用安全但速度较慢的协议来实现,而另一个不安全但速度更快.

For example, there could be two different namespaces, (making these up) System.Secure and System.Insecure. Both could have the classes for dealing with http with exactly the same names, but one could implement using secure but slower protocols, and the other insecure but quicker ones.

开发人员可以轻松地在它们之间进行选择,而无需在整个代码中重命名引用.或者可以在运行时使用反射更轻松地切换代码.

The developer could choose between them easily, without having to rename references all over the code. Or the code could be switched more easily at runtime, using reflection.

这篇关于如果导入父命名空间,为什么要导入子命名空间?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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