USING ‘using’ keyword with a namespace and as alias

There are several ways for using ‘using’ keyword in c Sharp with Namespaces which could change the context of the class in use dramatically. Consider the following three Namespaces. Two of them are independent, of which one contains another nested Namespace

Notice thet a simple declarartion use changes the context for class named ‘A’ found in tempNameSpace and another declaration ‘A’ which is an alias and has the type tempNameSpace.NamespaceChecker’


using System;
namespace usingSample
{
// using Alias for a namespace
using A = tempNameSpace.NamespaceChecker;
class Program
{
static void Main(string[] args)
{
/* calling A, which is actually belongs to
* tempNameSpace.NamespaceChecker in this very context
* */
A a = new A();
}
}
// Declaring Nested Namespace
namespace Nested
{
class A
{
public string printName(object type)
{ return type.GetType().FullName; }
}
class B { }
}
}
namespace tempNameSpace
{
// Using Nested Namespace
using usingSample.Nested;
class NamespaceChecker
{
public override string ToString()
{
/* calling A, which is actually belongs to
* usingSample.Nested namespace in this very context
* */
A a = new A();
string typeName = a.printName(this);
return typeName;
}
}
}

Advertisements