Prev | Current Page 371 | Next

Jon Skeet

"C# in Depth: What you need to master C# 2 and 3"

Windows.Forms;
using WebForms = System.Web.UI.WebControls;
class Test
{
static void Main()
{
Console.WriteLine (typeof (WinForms.Button));
Console.WriteLine (typeof (WebForms.Button));
}
}
Listing 7.5 compiles without any errors or warnings, although it??™s still not as pleasant
as it would be if we only needed to deal with one kind of Button to start with. There??™s
a problem, however??”what if someone were to introduce a type or namespace called
WinForms or WebForms? The compiler wouldn??™t know what WinForms.Button meant,
and would use the type or namespace in preference to the alias. We want to be able to
tell the compiler that we need it to treat WinForms as an alias, even though it??™s available
elsewhere. C# 2 introduces the ::syntax to do this, as shown in listing 7.6.
Listing 7.5 Using aliases to distinguish between different Button types
195 Namespace aliases
using System;
using WinForms = System.Windows.Forms;
using WebForms = System.Web.UI.WebControls;
class WinForms
{
}
class Test
{
static void Main()
{
Console.WriteLine (typeof (WinForms::Button));
Console.WriteLine (typeof (WebForms::Button));
}
}
Instead of WinForms.Button, listing 7.6 uses WinForms::Button, and the compiler is
happy. If you change the :: back to . you??™ll get a compilation error. So, if you use ::
everywhere you use an alias, you??™ll be fine, right? Well, not quite??¦
7.


Pages:
359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383