Chapter 13. Additional OOP Techniques

In this chapter, you continue exploring the C# language by looking at a few bits and pieces that haven't quite fit in elsewhere. This isn't to say that these techniques aren't useful—just that they don't fall under any of the headings you've worked through so far.

Specifically, you will look at the following:

  • 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 in 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. This may be necessary if you want to use a namespace alias and there is ambiguity between the alias and the actual namespace hierarchy. If that's the case, then the namespace hierarchy is given priority over the namespace alias. 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 might use the following to refer to a class:

MyNamespaceAlias.MyClass

The class referred to by this code is the MyRootNamespace.MyNamespaceAlias.MyClass class, not the MyRootNamespace.MyNestedNamespace.MyClass class. That is, the namespace MyRootNamespace.MyNamespaceAlias ...

Get Beginning Microsoft® Visual C#® 2008 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.