This program is given in following website:
http://msdn.microsoft.com/en-us/library/sf0df423(v=vs.80).aspx
Two questions are there
1) Even though nameSpace2 and using directives in nameSpace3 (using NameSpace1 and using NameSpace2;) are comment out program is still executing.
2) We have to use somevar.ToString() to access the ToString() method but here only "somevar" is used to access the ToString() method.
// cs_using_directive2.cs
// Using directive.
using System;
// Using alias for a class.
using AliasToMyClass = NameSpace1.MyClass;
namespace NameSpace1
{
public class MyClass
{
public override string ToString()
{
return "You are in NameSpace1.MyClass";
}
}
}
/*namespace NameSpace2
{
class MyClass
{
}
}*/
namespace NameSpace3
{
//using NameSpace1;
//using NameSpace2;
class MainClass
{
static void Main()
{
AliasToMyClass somevar = new AliasToMyClass();
Console.WriteLine(somevar);
}
}
}
Loading
Posted Dec 5, 2011, 12:39 PM
VulpesPosted Dec 5, 2011, 11:21 AM
AliasToMyClass is declared globally i.e. outside any namespace but is an alias for NameSpace1.MyClass so there's no doubt which class it's referring to even if NameSpace2 were not commented out.
Namespace3 doesn't need using statements for NameSpace1 and Namespace2 - in fact if it did have the latter, there would be a compiler error because Namespace2 is commented out.
It uses the globally declared AliasToMyClass to access NameSpace1.MyClass.
When you pass an argument to Console.WriteLine then, if it's not already a string, its ToString() method is called automatically - you don't have to specify it yourself but it does no harm if you do.