Implicit typing of arrays that implement interfaces

Posted by Sir Psycho on Stack Overflow See other posts from Stack Overflow or by Sir Psycho
Published on 2010-05-10T01:40:30Z Indexed on 2010/05/10 1:48 UTC
Read the original article Hit count: 377

Filed under:
|

Hi,

I was under the impression that the C# compiler will implicitly type an array based off a type that they can all be implicitly converted to.

The compiler generates No best type found for implicitly-typed array

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 };
}

I know that the way to correct this is to declare the type like:

new ISomething [] { obj1, ...} 

But I'm after an under the covers type help here :-)

Thanks

© Stack Overflow or respective owner

Related posts about c#

Related posts about implicit-typing