我原本认为C#编译器会隐式地根据可隐式转换的类型自动推断数组的类型。
但实际上,编译器生成了无法找到隐式类型的最佳匹配的错误提示。
public interface ISomething {}
public interface ISomething2 {}
public interface ISomething3 {}
public class Foo : ISomething { }
public class Bar : ISomething, ISomething2 { }
public class Car : ISomething, ISomething3 { }
void Main()
{
var obj1 = new Foo();
var obj2 = new Bar();
var obj3 = new Car();
var objects= new [] { obj1, obj2, obj3 };
}
我知道解决这个问题的方法是声明类型,例如:
new ISomething [] { obj1, ...}
但我需要的是一种“幕后”类型的帮助。