相关疑难解决方法(0)

实现接口的数组的隐式类型

我的印象是C#编译器将隐式地键入一个基于一个类型的数组,它们都可以隐式转换为.

编译器为隐式类型数组生成 No best type

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 };
}
Run Code Online (Sandbox Code Playgroud)

我知道纠正这种情况的方法是声明类型:

new ISomething [] { obj1, ...} 
Run Code Online (Sandbox Code Playgroud)

但是我在这里有一个封面类型的帮助.

c# implicit-typing

9
推荐指数
1
解决办法
6701
查看次数

标签 统计

c# ×1

implicit-typing ×1