C# Using Alias ExampleThis C# article describes how to use the using alias feature. This feature helps resolve ambiguities.
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.
The using alias directive syntax requires the "using" keyword and then the equals sign. Then an existing type name or namespace is required. Here we map the type "Cat" to the type "System.Text.StringBuilder".
Then: In the program, the type Cat can be used as a StringBuilder. This example makes the program more confusing.
C# program that shows using alias directive
using Cat = System.Text.StringBuilder;
static void Main()
Cat cat = new Cat();
Drawback. This can be used to create extremely confusing and non-standard programs. If you want to create extremely confusing programs, this feature can help. But don't expect to be admired for this goal.
What are some practical uses of the using alias directive? Sometimes, an entire program would want to use one type implementation for DEBUG mode, and a different implementation for RELEASE mode.
And: You could use an alias instead of the actual type names. Then you could use #ifdef and #else to wrap the using alias directive.DirectivesIf, Elif, Endif
Say you have a custom StringBuilder type in the namespace Perls.Animals.StringBuilder. If you include System.Text and this other namespace, the using alias directive can specify what StringBuilder actually points to.StringBuilder
As with the extern alias directive, the using alias directive is used to resolve ambiguities in programs. It can also enable you to swap implementations for a type based on compile-time flags.Extern
Warning: The major drawback is that it can be abused to create programs that are extremely confusing.
© 2007-2020 Sam Allen. Every person is special and unique. Send bug reports to email@example.com.