Given the following class:
public class Foo {
public Foo(int i, double d) {
Integer = i;
Double = d;
}
public int Integer {get;}
public double Double {get;}
private static Random rand = new Random();
public static Foo CreateRandom() => new Foo(rand.Next(1,101), rand.NextDouble());
}
As well as this usage:
void Main()
{
var items = Enumerable.Range(0, 50)
.Select(_ => Foo.CreateRandom());
Console.WriteLine(items.Sum(GetInteger)); // Fine
Console.WriteLine(items.Sum(GetDouble)); // Ambiguous
Console.WriteLine(items.Sum(x => x.Double)); // Also fine
Console.WriteLine(items.Sum((Func<Foo,double>)GetDouble)); // Cast required? Why?
int GetInteger(Foo item) => item.Integer;
double GetDouble(Foo item) => item.Double;
}
I am trying to figure out why it is that the GetDouble
delegate conversion is considered ambiguous, and what exactly makes it different in this context from the labmda expression and the cast to the anonymous delegate.
Edit: It looks like this does not affect C# 7.3, but does affect versions 7.2 and lower. Versions before local methods were added can be affected by making the GetInteger and GetDouble static.