The following code causes a compiler error, as it is ambiguous call but the problem if we use object instead of ArrayList no error happens and the string version works fine; Do you have an explanation for that?
class A
{
public A(string x)
{
Console.WriteLine("string");
}
public A(ArrayList x)
{
Console.WriteLine("ArrayList");
}
}
static void Main(string[] args)
{
A o = new A(null);
}
The reason your code works fine if you change the constructor that takes an
ArrayListto take anobjectis that the C# compiler will pick the most specific type applicable. In the case ofstring/object,stringactually derives fromobjectand is therefore “more specific” and will be inferred by the compiler. WithstringversusArrayList, it’s apples and oranges: either can be null, but neither is more “specific” than the other.