Ivandro Jao
Ivandro Jao

Reputation: 2961

C# using NamesSpaces

File 1
namespace ivandro.ismael.gomes
{
    class MyClass1
    {
        MyClass obj = new MyClass();
    }
}
File 2
namespace ivandro.ismael
{
    class MyClass
    {
    }
}

Note: MyClass will be visible to MyClass1 without saying using ivandro.ismael but if you say using System.Text, only the types in System.Text will be visible not the types inside the System Do you know why?!

Upvotes: 1

Views: 126

Answers (1)

Scott Chamberlain
Scott Chamberlain

Reputation: 127603

This is because code inside a namespace block behaves differently than just putting a using statement. You could visualize your MyClass1 code like the following.

namespace ivandro
{   
    namepace ismael
    {
        namepsace gomes
        {    
             class MyClass1
             {
                 MyClass obj = new MyClass();
             }
        }           
    }
}

So when you are inside a namespace all of the levels leading up to that namespace are included also.

Upvotes: 7

Related Questions