Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why doesn't this trigger an "Ambiguous Reference Error"?

Tags:

c#

public class A
{
    public virtual string Go(string str) { return str; }
    }

public class B : A
{
    public override string Go(string str) {return base.Go(str);}
    public string Go(IList<string> list) {return "list";}
}

public static void Main(string[] args)
{
    var ob = new B();
    Console.WriteLine(ob.Go(null));
}

http://dotnetpad.net/ViewPaste/s6VZDImprk2_CqulFcDJ1A

If I run this program I get "list" sent out to the output. Why doesn't this trigger an ambiguous reference error in the compiler?

like image 871
Vadim Avatar asked Mar 09 '11 01:03

Vadim


1 Answers

Since the overload taking a string is not defined in B (only overriden), it has lower precedence than the one taking an IList<string>.

Therefore, the second overload wins and there's no ambiguity.

This is explained in detail in http://csharpindepth.com/Articles/General/Overloading.aspx

like image 97
Diego Mijelshon Avatar answered Oct 29 '22 04:10

Diego Mijelshon