O'Reilly logo

Beginning Microsoft® Visual C#® 2008 by Eric White, Morgan Skinner, Jon D. Reid, Jacob Hammer Pedersen, Christian Nagel, Karli Watson

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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 ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required