Common Type System—Namespace Resolution


Jump to: navigation, search
Visual C# Tutorials
.NET Framework Tutorials

Common Type System

© 2006 Wiley Publishing Inc.

Namespace Resolution

Different languages resolve references to types inside namespaces in different manners. For example, in C# you can declare that a certain scope uses a namespace with the using keyword (Imports in VB), and you immediately get to access the types within it without having to type the fully qualified names. Without this feature, you’d need to type all of your source files as follows:

class Foo
{
   void Bar()
   {
      System.Collections.Generic.List<int> list =
         new System.Collections.Generic.List<int>();
      // ...
   }
}

Thankfully with the using keyword you can eliminate having to type so much:

using System.Collections.Generic;
 
class Foo
{
   void Bar()
   {
      List<int> list = new List<int>();
      // ...
   }
}

Sometimes imported namespaces can have types whose names conflict defined within them. In such cases, you must resort to fully qualified type names. To alleviate this problem, C# enables you to alias namespaces so that you can save on typing. For example, imagine we imported the Microsoft.Internal.CrazyCollections namespace, which just happened to have a List<T> type in it. This would conflict with System.Collections.Generic and must be disambiguated:

using SysColGen = System.Collections.Generic;
using Microsoft.Internal.CrazyCollections;
 
class Foo
{
   void Bar()
   {
      SysColGen.List<int> list = new SysColGen.List<int>();
      // ...
   }
}


Previous_Page_.gif Next_Page_.gif





Personal tools