C# Aliasing Types and Namespaces
In this chapter you will learn:
Description
We can provide an alias for a namespace with = sign.
using System;/*from ww w . j a va 2 s .co m*/
using MyAlias = A;
namespace A{
class ClassA{
}
namespace B{
class classB : ClassA{
}
}
}
class Program
{
static void Main(string[] args)
{
new MyAlias.ClassA();
}
}
The code above generates the following result.
Rename the namespaces
Rename the namespaces
using ThatConsoleClass = System.Console;
/*from w ww . j a v a2s.c om*/
class MainClass
{
public static void Main()
{
ThatConsoleClass.WriteLine("Hello");
}
}
The code above generates the following result.
Demonstrate the :: qualifier
How to use the :: qualifier
using System; //from w w w.jav a 2 s . c o m
using Counter;
using AnotherCounter;
// Give Counter an alias called Ctr.
using Ctr = Counter;
namespace Counter {
class MyClass {
}
}
namespace AnotherCounter {
class MyClass {
}
}
class MainClass {
public static void Main() {
Ctr::MyClass m = new Ctr::MyClass();
}
}
The code above generates the following result.
Next chapter...
What you will learn in the next chapter: