daniel mark

Hello all:

I read the following statement from MSDN

http://msdn2.microsoft.com/en-us/library/sf0df423(vs.71).aspx

A using directive does not give you access to any namespaces that may be nested in the namespace you specify.

What is the real meaning behind this statement

I tested the following code and it works !

Thank you

-Daniel

Code Snippet

// cs_using_directive.cs

using MyCompany.Proj.Nested;

namespace MyCompany.Proj
{

public class MyClass
{

public static void DoNothing()
{
}
}

namespace Nested // a nested namespace
{
public class ClassInNestedNameSpace
{
public static void SayHello()
{
System.Console.WriteLine("Hello");
}
}
}
}

public class UnNestedClass
{
public static void Main()
{
ClassInNestedNameSpace.SayHello(); // access function through using directive
}
}



Re: Visual C# Language question about using directive

Mattias Sjogren

I believe they simply mean that if you specify

usign System;

that will not give you access to types in sub-namespaces like System.Data.






Re: Visual C# Language question about using directive

bmains

Hey,

It means that if you specify using System.Collections, it does not automatically give you access to all the classes in using System.Collections.Generic; rather, you have to specify both if you need to access both.

Brian





Re: Visual C# Language question about using directive

michhes

...just to add to this... Namespaces don't offer any form of visibility modifiers. Your example works because your namespace declarations are equivalent to this:

Code Snippet

namespace MyCompany.Proj
{

public class MyClass { ... }

}

namespace MyCompany.Proj.Nested
{
public class ClassInNestedNameSpace { ... }
}






Re: Visual C# Language question about using directive

James Curran

Ok, let us assume that you "MyCompany.Proj" is, as defined in you message, but instead of

using MyCompany.Proj.Nested;

we wrote just

using MyCompany.Proj;

Then we could write

MyClass myc = new MyClass();

because the "using" gave us access to the namespace.

However we could not write:

ClassInNestedNameSpace cinns = new ClassInNestedNameSpace();

nor could we write:

Nested.ClassInNestedNameSpace cinns = new Nested.ClassInNestedNameSpace();

because the "using" does not give us access to the nested namespace, only the specified namespace.

We would have to write either:

using MyCompany.Proj.Nested;

ClassInNestedNameSpace cinns = new ClassInNestedNameSpace();

or

MyCompany.Proj.Nested.ClassInNestedNameSpace cinns = new MyCompany.Proj.Nested.ClassInNestedNameSpace();