简体   繁体   中英

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?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM