C# namespace alias - what's the point?

c# type alias
c# extern alias
c# alias variable
c# global namespace
c# using namespace
c# namespaces best practices
c# class alias
how to call namespace in c#

Where or when would one would use namespace aliasing like

 using someOtherName =  System.Timers.Timer;

It seems to me that it would just add more confusion to understanding the language.

That is a type alias, not a namespace alias; it is useful to disambiguate - for example, against:

using WinformTimer = System.Windows.Forms.Timer;
using ThreadingTimer = System.Threading.Timer;

(ps: thanks for the choice of Timer ;-p)

Otherwise, if you use both System.Windows.Forms.Timer and System.Timers.Timer in the same file you'd have to keep giving the full names (since Timer could be confusing).

It also plays a part with extern aliases for using types with the same fully-qualified type name from different assemblies - rare, but useful to be supported.


Actually, I can see another use: when you want quick access to a type, but don't want to use a regular using because you can't import some conflicting extension methods... a bit convoluted, but... here's an example...

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) { }
    }
}

using directive, To create an alias for a namespace or a type. This is called a using alias directive . C# Copy. An extern alias. The global alias, which is the global namespace alias. The global namespace is the namespace that contains namespaces and types that are not declared inside a named namespace. When used with the :: qualifier, the global alias always references the global namespace, even if there is the user-defined global namespace alias.

I use it when I've got multiple namespaces with conflicting sub namespaces and/or object names you could just do something like [as an example]:

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

...

src.DataObject source = new src.DataObject();
dst.DataObject destination = new dst.DataObject();

Which would otherwise have to be written:

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

Namespace2.Subspace.DataAccessObjects.DataObject dstination = 
  new Namespace2.Subspace.DataAccessObjects.DataObject();

It saves a ton of typing and can be used to make code a lot easier to read.

:: operator, The global alias, which is the global namespace alias. The global namespace is the namespace that contains namespaces and types that are not� Using Alias. A using alias directive introduces new type names. These point to any existing type or namespace. This provides more flexibility should the implementation need to change. This feature has a limited range of use in the C# language.

In addition to the examples mentioned, type aliases (rather than namespace aliases) can be handy when repeatedly referring to generic types:

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

private void DoStuff(Dictionary<string, SomeClassWithALongName> dict) {}

Versus:

using FooDict = Dictionary<string, SomeClassWithALongName>;

FooDict foo = new FooDict();

private void DoStuff(FooDict dict) {}

C# namespace alias - what's the point?, That is a type alias, not a namespace alias; it is useful to disambiguate - for example, against: using WinformTimer = System.Windows.Forms. Namespace Alias Qualifier(::) makes the use of alias name in place of longer namespace and it provides a way to avoid ambiguous definitions of the classes. It is always positioned between two identifiers. The qualifier looks like two colons(::) with an alias name and the class name. It can be global.

Brevity.

There are fringe benefits to provide clarity between namespaces which share type names, but essentially it's just sugar.

Namespace Aliases in C#, The alias lives within the scope of the namespace it Is declared in. When the code compiles, a token is created for the fully qualified name and the� C# - Namespaces - A namespace is designed for providing a way to keep one set of names separate from another. The class names declared in one namespace does not conflict with the

I always use it in situations like this

using Utility = MyBaseNamespace.MySubNamsepace.Utility;

where Utility would otherwise have a different context (like MyBaseNamespace.MySubNamespace.MySubSubNamespace.Utility), but I expect/prefer Utility to always point to that one particular class.

Type and Namespace Aliasing for C#, Type aliasing is a little known feature for C# and it comes in handy when you would like to alias a specific method or class from a namespace� Namespaces in C# serve two main purposes: to organize classes and functionality and to control the scope of classes and methods. Type aliasing is a little known feature for C# and it comes in handy when you would like to alias a specific method or class from a namespace for the sake of clarity.

C# Using Alias Example, A using alias directive introduces new type names. These point to any existing type or namespace. This provides more flexibility should the implementation need� Namespace aliases allow the programmer to define an alternate name for a namespace. They are commonly used as a convenient shortcut for long or deeply-nested namespaces.

Namespace aliases in C#, Let's see the example. There is class called SomeClass in two different namespaces and the class is instantiated in Main() method. Run the demo and see what� Namespace aliases serve the following purposes: Disambiguation – Aliases make the namespace in use clearer in code. Managing conflicting extension methods – If conflicts exist between namespaces, an alias provides a workaround for access; for example, a namespace preventing the visibility of another.

Creating Type Aliases in C#, This allows you to disambiguate type names without referencing an entire namespace. Again, the using directive adds the alias for the scope of�

Comments
  • How about a system wide using int = System.Int32 in C#? Useful, isn't it? Its the same use that can be taken advantage of elsewhere.
  • @nawfal I believe type aliases are not exportable. Meaning you cannot define something like using int = System.Int32, and use it in places other than the declaring file. So this int to Int32 alias may either be achieved by some other means, or is a special thing in the compiler/runtime.
  • @KFL that's true, but the benefit both provide is of the same nature.
  • @nawfal your argument about using int = System.Int32 is both wrong and misleading - it's wrong because int alias is not implemented the way you described. It's misleading because you imply type aliases can be used globally, just as how int is used over Int32.
  • @KFL i didnt imply both. I just stated why having a custom name for a type could be useful.
  • It can be used to alias either namespaces or type names.
  • @Sean: yes, but the example given was to a type
  • @lupefiasco: convenient of the OP to choose System.Timers.Timer ;-p
  • Ah, thought you were referring to the concept and not the specific example. Mea culpa.
  • It clearly shows which symbol you are using. It is not just sugar, but a little bit verbose (if you don't want to define a new name).
  • Don't you find that often the alias has more to do with the context in which it is being used than the physical location of the object?