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

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s