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:

image

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

image

Get C# 4.0 Unleashed now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.