Using Aliases

The using directive can be extended with the definition of a named alias, acting as an abbreviation for the namespace or type name on the right side. An example of this is shown in Chapter 22, “Dynamic Programming,” when dealing with the Word and Excel automation APIs. In this context, the Application type exists in both namespaces used for Word and Excel interop. Use of an alias allowed us to abbreviate references to them:

using Word  = Microsoft.Office.Interop.Word;using Excel = Microsoft.Office.Interop.Excel;

Now we can write the following two lines of code, disambiguating a reference to the Application interface:

var excel = new Excel.Application();var word  = new Word.Application();

Defined aliases ...

Get C# 5.0 Unleashed now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.