C# supports the concept of namespaces aliases for a long time. This allows you to create an alias for long namespaces so you don’t have to specify the whole name every time.
An example:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
namespace PC.MyCompany.Project | |
{ | |
class Product | |
{ | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using Project = PC.MyCompany.Project; | |
namespace Example | |
{ | |
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
var product=new Project.Product(); | |
Console.ReadKey(); | |
} | |
} | |
} |
Did you know that this does not only work for namespaces but also for classes? It is also possible to alias a specific class name.
An example:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//Don't do this at home and choose a non-confusing alias name ;-) | |
using Order = PC.MyCompany.Project.Product | |
namespace Example | |
{ | |
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
var order=new Order(); | |
Console.ReadKey(); | |
} | |
} | |
} |
This feature could be fun if you start using aliases that match with class names used somewhere else in your codebase. Like in the example above where I assigned the Product class an Order alias. Can’t be any more confusing! So please don’t do this at work and choose a good alias name instead…