2

How does the .NET compiler (and then the CLR at runtime) determine which class to use when we define the same namespace and class name in two different assemblies, without using extern alias to help it decide? Consider this sample - all in a single .cs file:

using System;

namespace Nonsense
{
    class Program
    {
        static void Main(string[] args)
        {
            String s = new System.String();
        }
    }
}

namespace System
{
    public class String
    {
        // Nothing here
    }
}

This code compiles without any errors. At runtime, this code uses the empty System.String shown above instead of the one from the framework. Why? Is this deterministic and, if so, what's the rule? Moving the empty implementation of System.String above into another assembly and referencing it does the same thing - Main sees the empty one. Why? How does it choose to use the empty one in a custom assembly vs. the "real" one in the framework's System assembly?

Clay Fowler
  • 2,059
  • 13
  • 15
  • `At runtime, this code uses the empty System.String shown above instead of the one from the framework. Why?` -- Probably because it's in the same assembly as the namespace declaration, and it's shadowing the one in the foreign assembly. I would imagine that the compiler will *always* use the local type *first.* – Robert Harvey Aug 21 '14 at 22:57
  • Because you are naming a class String within same assembly. So class named in same assembly will take precedence over external assemblies. – J. Davidson Aug 21 '14 at 23:00
  • 1
    `Moving the empty implementation of System.String above into another assembly and referencing it does the same thing` - I seem to be getting a compile time error here doing that. Does that definitely work for you? – petelids Aug 21 '14 at 23:31
  • You're right petelids, at least on Visual Studio 2013 with framework 4.5 - I edited the question. – Clay Fowler Aug 22 '14 at 16:04
  • @J.Davidson - if that's the answer, then great. But how do we know that's the answer, all of the time, every time? Is that in the C# spec somewhere or any other official documentation? – Clay Fowler Aug 22 '14 at 16:05
  • Does this answer your question? [Should 'using' directives be inside or outside the namespace?](https://stackoverflow.com/questions/125319/should-using-directives-be-inside-or-outside-the-namespace) – nalka Mar 25 '21 at 13:25

2 Answers2

1

Did you check Warnings? I have the following warnings:

Warning CS1685: The predefined type System.String' is defined multiple times. Using definition from mscorlib.dll' (CS1685)

Warning CS0436: The type `System.String' conflicts with the imported type of same name'. Ignoring the imported type definition (CS0436)

And details about first one is the answer: http://msdn.microsoft.com/en-us/library/8xys0hxk.aspx

Community
  • 1
  • 1
yesenin
  • 199
  • 7
1

It has to do with your usingdirectives. Where they are placed affects how references are resolved. If a using directive is placed outside a namespace declaration, that namespace's contents are loaded into the global namespace. If, however, the using directive is placed within a namespace declaration, its contents are loaded into that namespace.

The global namespace is searched last.

You can control this somewhat by using the global namespace alias as described at http://msdn.microsoft.com/en-us/library/c3ay4x3d.aspx

See also

Community
  • 1
  • 1
Nicholas Carey
  • 71,308
  • 16
  • 93
  • 135