c# - 故障排除 : Why doesn't type inference fail here?

标签 c# generics .net-4.0 type-inference

给定一个如下所示声明的类型

public class EqualityProbe<T>
{
  public EqualityProbe( Func<T> functionToGetActualValue, T expectedValue, string probeDescription) {..}

客户端代码:

// cannot infer bool here
new EqualityProbe(CanConnectToMachine, true, "Probe machine is online")
// compiles fine
new EqualityProbe<bool>(CanConnectToMachine, true, "Probe machine is online")

我的理解是类型推断不适用于方法组(例如 CanConnectToMachine)或匿名方法(lambda 表达式)。
但是在这种情况下,为什么编译器不从第二个参数中推断出类型参数

最佳答案

C# 不支持构造函数的类型推断,尽管这通常可以通过使用工厂类来克服。

在这里查看答案:Why can't the C# constructor infer type?

关于c# - 故障排除 : Why doesn't type inference fail here?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5725938/

相关文章:

c# - WCF 客户端异常 : error while trying to deserialize parameter

sql - 在oracle中编写通用过程

winforms - 在 Windows 窗体中仅显示具有透明度的图像?

.net - WCF内部队列监控(为了避免向服务发送太多请求)

c# - 基于 JsonSerializerSettings 确定 JSON.Net 属性映射

c# - 将程序集加载到 AppDomain 但出现无法加载文件或程序集异常

c# - 通过 .NET 运行 cmd 命令?

java - 为什么 Java 编译器在构造函数实例化中会丢失对泛型类型的跟踪?

c# - 如何缩短 List<List<KeyValuePair<string, string>>>?

silverlight - 需要旧版本的 silverlight