13

Additional OOP Techniques

In this chapter, you round off your discussion on the C# language by looking at a few bits and pieces that haven't quite fitted in elsewhere. This isn't to say that these techniques aren't useful — just that they haven't fallen under any of the headings you've worked through so far.

Specifically, you will look at:

  • The :: operator and the global namespace qualifier
  • Custom exceptions and exception recommendations
  • Events
  • Anonymous methods

You also make some final modifications to the CardLib code that you've been building up throughout the last few chapters, and even use CardLib to create a card game.

The :: Operator and the Global Namespace Qualifier

The :: operator provides an alternative way to access types in namespaces, where namespace aliases are given priority over the usual type qualification. To see what this means, consider the following code:

using MyNamespaceAlias = MyRootNamespace.MyNestedNamespace;

namespace MyRootNamespace
{
   namespace MyNamespaceAlias
   {
      public class MyClass
      {
      }
   }

   namespace MyNestedNamespace
   {
      public class MyClass
      {
      }
   }
}

Code in MyRootNamespace can access MyRootNamespace.MyNamespaceAlias.MyClass as follows:

MyNamespaceAlias.MyClass

That is to say that MyRootNamespace.MyNamespaceAlias has hidden the alias defined by the using statement, which refers to MyRootNamespace.MyNestedAlias . You can still access this namespace, and the class contained within, but you require different syntax:

MyRootNamespace.MyNamespaceAlias.MyClass ...

Get Beginning Visual C#® 2005 now with the O’Reilly learning platform.

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