C#名称空间别名 - 有什么意义?

Bra*_*rad 88 c# namespaces

我一直在努力学习更多关于C#语言的知识,但是我还没有看到一个人会使用命名空间别名的情况.

 using someOtherName =  System.Timers.Timer;
Run Code Online (Sandbox Code Playgroud)

在我看来,它只会增加对理解语言的困惑.有人可以解释一下.

谢谢

Mar*_*ell 143

这是一个类型别名,而不是命名空间别名; 消除歧义很有用 - 例如,反对:

using WinformTimer = System.Windows.Forms.Timer;
using ThreadingTimer = System.Threading.Timer;
Run Code Online (Sandbox Code Playgroud)

(ps:感谢选择Timer;-p)

否则,如果您同时使用System.Windows.Forms.Timer,并System.Timers.Timer在同一文件中,你不得不继续给的全名(因为Timer可能会造成混淆).

它还与extern别名一起使用,以便在不同的程序集中使用具有相同完全限定类型名称的类型 - 很少见,但是很有用.


实际上,我可以看到另一个用途:当你想快速访问一个类型,但不想使用常规using因为你不能导入一些冲突的扩展方法...有点复杂,但是......这是一个例子...

namespace RealCode {
    //using Foo; // can't use this - it breaks DoSomething
    using Handy = Foo.Handy;
    using Bar;
    static class Program {
        static void Main() {
            Handy h = new Handy(); // prove available
            string test = "abc";            
            test.DoSomething(); // prove available
        }
    }
}
namespace Foo {
    static class TypeOne {
        public static void DoSomething(this string value) { }
    }
    class Handy {}
}
namespace Bar {
    static class TypeTwo {
        public static void DoSomething(this string value) { }
    }
}
Run Code Online (Sandbox Code Playgroud)

  • 它可以用于命名空间名称或类型名称. (8认同)
  • @Sean:是的,但是给出的例子是一个类型 (2认同)

Ben*_*ter 25

当我有多个具有冲突的子命名空间和/或对象名称的命名空间时,我可以使用它,你可以像[作为一个例子]做一些事情:

using src = Namespace1.Subspace.DataAccessObjects;
using dst = Namespace2.Subspace.DataAccessObjects;

...

src.DataObject source = new src.DataObject();
dst.DataObject destination = new dst.DataObject();
Run Code Online (Sandbox Code Playgroud)

否则必须写入:

Namespace1.Subspace.DataAccessObjects.DataObject source = 
  new Namespace1.Subspace.DataAccessObjects.DataObject();

Namespace2.Subspace.DataAccessObjects.DataObject dstination = 
  new Namespace2.Subspace.DataAccessObjects.DataObject();
Run Code Online (Sandbox Code Playgroud)

它节省了大量的打字,可以用来使代码更容易阅读.


Joe*_*ler 17

除了上面提到的示例之外,在重复引用泛型类型时,类型别名(而不是命名空间别名)可以很方便:

Dictionary<string, SomeClassWithALongName> foo = new Dictionary<string, SomeClassWithALongName>();

private void DoStuff(Dictionary<string, SomeClassWithALongName> dict) {}
Run Code Online (Sandbox Code Playgroud)

与:

using FooDict = Dictionary<string, SomeClassWithALongName>;

FooDict foo = new FooDict();

private void DoStuff(FooDict dict) {}
Run Code Online (Sandbox Code Playgroud)


ann*_*ata 8

简洁.

在共享类型名称的命名空间之间提供清晰度有一些附加好处,但实质上它只是糖.


bdu*_*kes 7

我总是在这样的情况下使用它

using Utility = MyBaseNamespace.MySubNamsepace.Utility;
Run Code Online (Sandbox Code Playgroud)

其中,Utility否则将有一个不同的环境(如MyBaseNamespace.MySubNamespace.MySubSubNamespace.Utility),但我希望/喜欢Utility总是指向一个特定的类.


Sea*_*ght 5

当您在多个包含的命名空间中有多个具有相同名称的类时,这将非常有用。例如...

namespace Something.From.SomeCompanyA {
    public class Foo {
        /* ... */
    }
}

namespace CompanyB.Makes.ThisOne {
    public class Foo {
        /* ... */
    }
}
Run Code Online (Sandbox Code Playgroud)

您可以使用别名使编译器满意,并使您和团队中的其他人更清楚:

using CompanyA = Something.From.CompanyA;
using CompanyB = CompanyB.Makes.ThisOne;

/* ... */

CompanyA.Foo f = new CompanyA.Foo();
CompanyB.Foo x = new CompanyB.Foo();
Run Code Online (Sandbox Code Playgroud)