在C#中使用命名空间 [英] Usage of namespaces in C#

查看:89
本文介绍了在C#中使用命名空间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

通过使用关键字定义命名空间之间的区别是什么,只是给出了在任何地方使用文件的路径?

What is the difference between defining a namespace by using keyword and simply giving the path to access the file wherever it is used ??

推荐答案

请注意,在使用类时,不要在代码中提供物理路径。在添加对程序集的引用时使用物理路径,但在引用类时使用命名空间路径(完全限定名称)。这与物理层无关(它在Java中)。

命名空间为代码提供了更多的逻辑层次结构,但也可以是特定于供应商的。如果你添加一个使用,你只需给出编译器类的快捷方式 - 这可能会带来模糊的引用。



但这三个是等价的:

Be aware, that you don't give physical path in the code when using a class. You use the physical path when you add reference to the assembly, but you use the namespace path (fully qualified name) when you refer to a class. That has nothing to do with the physical one (it does in Java).
Namespaces give more a logical hierarchy to the code, but can be vendor-specific also. And if you add a "using" you just give a shortcut to the class for the compiler - which can bring to ambiguous references though.

But these three are equivalent:
using System;

public class Example
{
   public static void Main()
   {
      Console.WriteLine("This is a text");
   }
}







public class Example
{
   public static void Main()
   {
      System.Console.WriteLine("This is a text");
   }
}







using sys = System;

public class Example
{
   public static void Main()
   {
      sys.Console.WriteLine("This is a text");
   }
}





请注意不要混淆使用的其他用法 keyword:使用语句 [ ^ ]。



And be aware not to confuse with the other usage of using keyword: the using statement[^].


按路径你可能是指该类型的全名,也称为完全限定名称。



答案是:没有区别。 using 子句只是用于缩写顶级类型名称的快捷方式。您可以自由组合使用(包括使用它来创建别名名称),完全是部分限定名称,以实现简洁并避免含糊不清。



-SA
By "path" you probably mean the "full name" of the type, also called fully-qualified name.

The answer is: no difference. The "using" clause is only a shortcut used for abbreviation of the names of top-level types. You can freely combine "using" (including using it to create alias names), fully an partially qualified name, to achieve brevity and avoid ambiguity.

—SA


使用意味着您不需要输出整个路径。它通常更容易。就表现而言,我不认为它有任何影响。
Using means you don't need to type out the whole path. It's just easier usually. As far as performance I don't believe it has any bearing whatsoever.


这篇关于在C#中使用命名空间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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