Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/306.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在C#中,为什么';t?:操作员使用lambda或方法组?_C#_Lambda - Fatal编程技术网

在C#中,为什么';t?:操作员使用lambda或方法组?

在C#中,为什么';t?:操作员使用lambda或方法组?,c#,lambda,C#,Lambda,不起作用: Func<string, byte[]> getFileContents = (Mode != null && Mode.ToUpper() == "TEXT") ? TextFileContents : BinaryFileContents; private static byte[] BinaryFileContents(string file) { return System.IO.File.

不起作用:

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());
    }
}
Func getFileContents=(Mode!=null&&Mode.ToUpper()=“TEXT”)
? 文本文件内容
:二进制文件内容;
私有静态字节[]二进制文件内容(字符串文件)
{
返回System.IO.File.ReadAllBytes(文件);
}
私有静态字节[]TextFileContents(字符串文件)
{
使用(var sourceStream=newstreamreader(文件))
{
返回Encoding.UTF8.GetBytes(sourceStream.ReadToEnd());
}
}
错误是

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());
    }
}
方法组和方法组之间没有隐式转换

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());
    }
}
有效:

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());
    }
}
Func<string, byte[]> getFileContents2;
if (Mode != null && Mode.ToUpper() == "TEXT")
{
   getFileContents2 = TextFileContents;
}
else
{
   getFileContents2 = BinaryFileContents;
}
Func getFileContents2;
if(Mode!=null&&Mode.ToUpper()=“TEXT”)
{
getFileContents2=TextFileContents;
}
其他的
{
getFileContents2=BinaryFileContents;
}

我只是好奇为什么?我遗漏了什么吗?

匿名函数和方法组本身没有类型-它们只是可转换为委托类型(以及某些lambda表达式的表达式树类型)

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());
    }
}
要使条件运算符确定表达式的整体类型,第二个或第三个操作数中必须至少有一个具有类型。您可以将其中一个强制转换为
Func
,编译器会发现它可以将另一个转换为相同的类型,并且非常高兴

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());
    }
}
例如:

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());
    }
}
Func<string, byte[]> getFileContents = DateTime.Now.Hour > 10
    ? (Func<string, byte[]>) TextFileContents
    : BinaryFileContents;
Func getFileContents=DateTime.Now.Hour>10
? (Func)TextFileContents
:二进制文件内容;
根据C#5规范第7.14节:

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());
    }
}
?运算符的第二个和第三个操作数x和y控制条件表达式的类型

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());
    }
}
  • 如果x的类型为x,y的类型为y,则[…]
  • 如果x和y中只有一个具有类型[…]
  • 否则,无法确定表达式类型,并发生编译时错误

很好,使用您的解决方案,您甚至可以切换到varI,直到现在,您还没有真正理解这一细微差别,“…本身没有类型-它们只是可转换为委托类型…”。价值远远超过+1。@billisphere:很高兴它有帮助:)我也花了很长时间才得到。(顺便说一句,
null
literal也是如此。大多数表达式都有类型。)@MaximeTremblay Savard:我的意思是,从开始学习C#开始。在我开始回答这个问题之前我就知道了:)@MaximeTremblay Savard Jon至少每月回答一次这个问题。。。你应该很容易找到Jon或Eric Lippert的精确副本