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?
The ' |= ' symbol is the bitwise OR assignment operator.
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 ...
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With