Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

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?

like image 648
Aaron Anodide Avatar asked Jun 26 '15 19:06

Aaron Anodide


People also ask

What does |= mean in C?

The ' |= ' symbol is the bitwise OR assignment operator.

What is '~' in C programming?

In mathematics, the tilde often represents approximation, especially when used in duplicate, and is sometimes called the "equivalency sign." In regular expressions, the tilde is used as an operator in pattern matching, and in C programming, it is used as a bitwise operator representing a unary negation (i.e., "bitwise ...

What is an operator in C?

C operators are one of the features in C which has symbols that can be used to perform mathematical, relational, bitwise, conditional, or logical manipulations. The C programming language has a lot of built-in operators to perform various tasks as per the need of the program.

What is the use of in C?

In C/C++, the # sign marks preprocessor directives. If you're not familiar with the preprocessor, it works as part of the compilation process, handling includes, macros, and more.


1 Answers

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.
like image 125
Jon Skeet Avatar answered Oct 15 '22 08:10

Jon Skeet