Reputation: 9393
The following C# code compiles and runs just fine:
private static IReadOnlyList<int> Foo()
{
return new int[10];
}
However, the (supposed) equivalent C++/CLI code gives a compile error:
Error C2440 'return': cannot convert from 'cli::array<int,1> ^' to 'System::Collections::Generic::IReadOnlyList<int> ^'
static IReadOnlyList<int>^ Foo()
{
return gcnew array<int>(10);
}
The error message makes it sound as though C++/CLI has its own special array class that it's using under the hood, which is different from what C# does and which doesn't implement IReadOnlyList (or IList, or ICollection; IEnumerable does work, though). Is that correct? Is there a workaround?
Upvotes: 2
Views: 1359
Reputation: 9393
Apparently all you need to do is add an explicit cast:
static IReadOnlyList<int>^ Foo()
{
return (IReadOnlyList<int>^)gcnew array<int>(10);
}
(I confirmed this does not blow up a runtime)
Upvotes: 2