Namespace Aliases In Visual Basic .NET

In Visual Basic .NET, it is possible to declare aliases for namespaces, giving the possibility of using more concise declarations, if the developer desires to do so.

Let's see an example: Typically, the use of StringBuilder class could follow the following three declarative ways:

Without Imports statement
  1. Dim s As New System.Text.StringBuilder("test")  
  2. s.Append("123")  
This kind of implementation requires the developer to declare the "s" variable writing down the path to the desired class, in our case StringBuilder, contained into System.Text namespace.

With Imports statement
  1. Imports System.Text  
  2. ' ...  
  3. Dim s As New StringBuilder("test")  
  4. s.Append("123")  
Using an Imports statement, the programmer can simply use the classes names, for the namespace(s) to be used will be declared at the beginning of the code.

Using Aliases
  1. Imports sb = System.Text.StringBuilder  
  2. '...  
  3. Dim s As New sb("test")  
  4. s.Append("123")  
The Imports statement could be implemented with customized names. In the example above, I have stated that in the rest of the code, the class System.Text.StringBuilder will be redefined as "sb". So, we can declare a new StringBuilder with the function "New sb()", accessing the same constructors possessed by System.Text.StringBuilder, being sb a simple alias of the real thing.