尽管我们导入了父名称空间,但为什么要使用子名称空间 [英] Why to use sub namespace although we hv imported parent namespace

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

问题描述

在导入名称空间时为什么要导入子名称空间,尽管我们已经导入了父名称空间....... ex-
使用系统;
使用system.data;
我们导入了系统,再次导入了数据,为什么呢?????,如果有任何想法,请提前共享........ thanx

解决方案

<首先,我们不是在这里导入"任何东西.如果愿意,"using"子句是用于解析名称的纯语法糖.同样,名称空间本身与程序集之间的关系无关-您可以在同一程序集中拥有不同的名称空间;并且在这种情况下,您是否会说程序集一次又一次导入"自身? :-)仅当您添加对项目的引用时,所有必要的东西才会发生.

因此,首先,这是完全相同的:

 使用 System.Data;

//  ... 

DataTable myTable; 

 System.Data.DataTable myTable; 



其次,仅当您构建的程序集引用程序集"System.Data.dll"(位于CAG中)时,才可以在此示例中使用类型.

最后,在诸如"System"和"System.Data"之类的名称空间中具有公共部分"System"绝对不能说明它们之间的关系.它们可能是相关的,也可能不是.命名空间的命名是完全不相关的.如我所说,同一程序集中可以有不同的名称空间,但是在完全不相关的程序集中也可以使用相同的名称空间.例如,您可以命名自己的命名空间"System.Data",并将其与程序集"System.Data.dll"中的名称空间一起使用.为了方便起见,仅出于一种目的对名称空间的名称进行了分组:名称解析.

—SA


while importing namespace why we import sub namespace although we have imported parent namespace.......ex-
using system;
using system.data;
We hv imported system,again we r importing data why???????,plz share if any idea ........thanx in advance

解决方案

First, we are not "importing" anything here. The "using" clause is a pure syntactic sugar used to resolve the names, if you will. Also, name spaces themselves have nothing to do with relationship between assemblies — you can have different name spaces in the same assembly; and, in this case, will you say that the assembly "imports" itself once and then again? :-) All essential stuff happens only when you add a reference to a project.

So, first, this is strictly the same:

using System.Data;

//...

DataTable myTable;

and

System.Data.DataTable myTable;



Secondly, using the type in this example is only possible when the assembly "System.Data.dll" (found in the CAG) is referenced by an assembly you build.

Finally, having a common part "System" in the name spaces like "System" and "System.Data" says absolutely nothing about their relationship. They might be related or they might be not. Name spaces naming is totally unrelated. As I say, you can have different name space in the same assembly, but you also can use the same name space in totally unrelated assembly. For example, you can name your own name space "System.Data" and use it with the one from the assembly "System.Data.dll". Names of the name spaces are grouped only for convenience for only one purpose: name resolution.

—SA


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

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