Tanmoy
Tanmoy

Reputation: 45632

Putting using statement inside the namespace fails

I was trying to get some of the old code properly styled with stylecop. It asks for putting the using statements inside. It worked well for all but one. I have reduced the problem to the below code.

namespace B.C
{
    using System;

    public class Hidden
    {
        public void SayHello()
        {
            Console.WriteLine("Hello");
        }
    }
}

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

    public class Program
    {
        static void Main(string[] args)
        {
            new Hidden().SayHello();
        }
    }
}

this gives compilation error Error

"The type or namespace name 'Hidden' could not be found (are you missing a using directive or an assembly reference?)".

If I move using B.C; above the namespace A.B.C then it builds properly. The class Hidden is developed by different team and we cannot modify it.

Upvotes: 11

Views: 1260

Answers (2)

Mr. Mr.
Mr. Mr.

Reputation: 4285

B.C is conflicting with A.B.C. You need to rename B.C to something else or specify it with global

Upvotes: 2

Guffa
Guffa

Reputation: 700152

As you are inside the namespace A, then B.C will actually be A.B.C.

Use global:: to specify that you are looking from the root:

using global::B.C;

Upvotes: 16

Related Questions