0

I have a question on function overloading, Can you please let me know why it is not showing compilation error

class Program
{
    static void Main(string[] args)
    {
        var inputObj = new CalcClass();
        Console.WriteLine(inputObj.AddNumber(null, null));
    }
}
class CalcClass
{
    public double AddNumber(double? a, double? b)
    {
        return (double)(a ?? 0 + b ?? 0);
    }
    public short AddNumber(short? a, short? b)
    {
        return (short)(a ?? 0 + b ?? 0);
    }
    public int AddNumber(int? a, int? b)
    {
        return (int)(a ?? 0 + b ?? 0);
    }
}

It is executing the public short AddNumber(short? a, short? b) function even though ambiguous between the class methods

Santosh_Sams
  • 207
  • 1
  • 2
  • 10
  • 3
    That's overloading, not polymorphism. – SLaks Mar 13 '18 at 14:56
  • I have update description now – Santosh_Sams Mar 13 '18 at 14:59
  • What makes you think it *is* ambiguous? – Fildor Mar 13 '18 at 15:02
  • 1
    "Given two different types T1 and T2, T1 is a better conversion target than T2 if at least one of the following holds: ... An implicit conversion from T1 to T2 exists, and no implicit conversion from T2 to T1 exists" – Damien_The_Unbeliever Mar 13 '18 at 15:07
  • Read [this](https://learn.microsoft.com/en-us/dotnet/visual-basic/programming-guide/language-features/procedures/overload-resolution). It has a nice flow chart on "How to pick your Overload?" With an exemple of byte short and int. Thats is really close to your test – Drag and Drop Mar 13 '18 at 15:07
  • @DragandDrop That's not even the same language. – itsme86 Mar 13 '18 at 15:10
  • @itsme86, And? You think the process is different? – Drag and Drop Mar 13 '18 at 15:12
  • @DragandDrop Why would you assume they're the same? If I'm going to change the spark plugs in my Ford, I'm not going to go to a Chevy manual for the gap information. – itsme86 Mar 13 '18 at 15:13
  • @DragandDrop - they do have different overloading and shadowing rules. Not relevant to this specific example but reasoning about one language by trying to use the other language's rules won't always work. – Damien_The_Unbeliever Mar 13 '18 at 15:14
  • @Fildor: try this Console.WriteLine((new CalcClass()).Print(null)); public string Print(IEnumerable names) { var sb = new StringBuilder(); foreach (var item in names) sb.Append(item + ", "); return sb.ToString(); } public string Print(string names) { var sb = new StringBuilder(); foreach (var item in names.Split(';')) { sb.Append(item + ", "); } return sb.ToString(); } – Santosh_Sams Mar 13 '18 at 15:15
  • @itsme86 When it comes to Picking an overload things can change depending of the version. I'm pretty you have read the specification. So you are aware of the complexity of the subject. The main goal of my comment was to provide a visual help to "how to pick an Overload". If you spot any difference between this article and ?? And what by the way as every major version can have different spec.. I remember the difference between c#4 and c#6 was about type inference and lambda. – Drag and Drop Mar 13 '18 at 15:19
  • @Damien_The_Unbeliever, this is why this is a comment and not an answer. Because the exact way to pick an overload is complex rules about lambda, anonymous method is not trivial. – Drag and Drop Mar 13 '18 at 15:23
  • @Santosh_Sams _That_ example has nothing to do with the one in the question. Those are two reference types. – Fildor Mar 13 '18 at 15:25
  • If we decide to be picky about this "simple vb" implementation. We have to decide to witch C# version and Framework we want to explain the spec. But op is not talking about something really specific that will require the spec. – Drag and Drop Mar 13 '18 at 15:34

0 Answers0