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?