How does C# know how to correctly modify namespace for it to work?

Viewed 121

How does C# know how to correctly modify namespace for it to work?

Recently I come across a seemingly simple concept, but I was struck by my lack of a clear understanding of what is going on. And a simple googling did not help me to get a better picture. So, I am making the question.

I have a project with two files:

File1.cs

namespace A.B.C
{
    using E.F;

    public class D 
    {
        G g;
    }
}

File2.cs

namespace A.B.E.F
{
    public class G { }
}

Now before my coworker pointed this out, I never met this, even though I read a few in depth books on C# and have a quite long experience working with C#. So, here the C# parser does a pretty clever thing. He takes the using E.F; from the File1.cs, takes the enclosing namespaces (i.e. the namespace A.B.C), removes the C from the enclosing namespace and adds the E.F to the remaining namespace part. Getting the using A.B.E.F in the end.

The modification does not seem trivial for me at all. The compiler should now how many trailing namespaces to remove from the A.B.C (in this case 1 - that is C), so that the addition of the E.F would lead to a namespace containing the class G.

Now I am asking if my understanding of what is happening is correct and where could I read about these modifications a compiler does?

UPDATE

If we take the following files:

File1.cs

namespace A.B.C
{
    using E.F;

    public class D 
    {
        G g;
    }
}

File2.cs

namespace A.B.E.F
{
    public class G { }
}

namespace E.F
{

}

The compiler will still be able to locate exactly the A.B.E.F in the File1.cs in the using E.F; line and not the E.F. So, the compiler is not simply looking for the E.F namespace as was pointed out in comments, but it looks for such E.F which would contain the class G. Correct me if I am wrong, please.

UPDATE 2

The comment which points to this (from the @Damien_The_Unbeliever) documentation made me think about the first versions of the File1.cs and File2.cs. In some sense the result they produce looks like that:

namespace A
{
    namespace B
    {
        namespace C
        {
            using E.F;

            public class D
            {
                G g;
            }
        }

        namespace E
        {
            namespace F
            {
                public class G { }
            }
        }
    }
}

Now we can move the using E.F up through the namespaces chain. The namespace E.F is defined starting with the namespace A.B, so the following is legit:

namespace A
{
    namespace B
    {
        using E.F;

        namespace C
        {
            public class D
            {
                G g;
            }
        }

        namespace E
        {
            namespace F
            {
                public class G { }
            }
        }
    }
}

While the following is not legit, because in namespace A we still do not know about the E.F namespace:

namespace A
{
    using E.F;

    namespace B
    {
        namespace C
        {
            public class D
            {
                G g;
            }
        }

        namespace E
        {
            namespace F
            {
                public class G { }
            }
        }
    }
}

But, we know about the B.E.F namespace in the namespace A, so the following is legit:

namespace A
{
    using B.E.F;

    namespace B
    {
        namespace C
        {
            public class D
            {
                G g;
            }
        }

        namespace E
        {
            namespace F
            {
                public class G { }
            }
        }
    }
}

So, my guess is that my first assumption about the C# computing the namespaces was wrong. And actually C# does not compute namespaces, but just knows a set of namespaces in each given namespace. And some namespaces may have more than one name in some cases. E.g. the A.B.E.F namespace has the following names inside the A.B namespace: A.B.E.F, B.E.F, E.F. All of the 3 namespace names point to a same namespace. Is my understanding correct?

0 Answers
Related