In C#, why doesn't ?: operator work with lambda or method groups?

Does not work:

Func<string, byte[]> getFileContents = (Mode != null && Mode.ToUpper() == "TEXT")
            ? TextFileContents
            : BinaryFileContents;

private static byte[] BinaryFileContents(string file)
{
    return System.IO.File.ReadAllBytes(file);
}

private static byte[] TextFileContents(string file)
{
    using (var sourceStream = new StreamReader(file))
    {
        return Encoding.UTF8.GetBytes(sourceStream.ReadToEnd());
    }
}

Error is

no implicit conversion between method group and method group

Works:

Func<string, byte[]> getFileContents2;
if (Mode != null && Mode.ToUpper() == "TEXT")
{
   getFileContents2 = TextFileContents;
}
else
{
   getFileContents2 = BinaryFileContents;
}

I'm just curious why? Am I missing something?

Jon Skeet
people
quotationmark

Anonymous functions and method groups don't have types in themselves - they are merely convertible to delegate types (and expression tree types for some lambda expressions).

For the conditional operator to determine the overall type of the expression, at least one of the second or third operands has to have a type. You could cast either of them to Func<string, byte[]> and the compiler would find that it could convert the other one to the same type, and be happy.

For example:

Func<string, byte[]> getFileContents = DateTime.Now.Hour > 10
    ? (Func<string, byte[]>) TextFileContents
    : BinaryFileContents;

From section 7.14 of the C# 5 spec:

The second and third operands, x and y, of the ?: operator control the type of the conditional expression.

  • If x has type X and y has type Y then [...]
  • If only one of x and y has a type [...]
  • Otherwise, no expression type can be determined, and a compile-time error occurs.

people

See more on this question at Stackoverflow