I have a method like
public IList<IList<int>> LevelOrderBottom(TreeNode root)
{
var result = new List<List<int>>();
// ...
return result;
}
and am getting compile-time error
Cannot implicitly convert type 'System.Collections.Generic.List>' to 'System.Collections.Generic.IList>'. An explicit conversion exists (are you missing a cast?)
I don't understand why that doesn't work, but
private static IList<int> ReturnAList()
{
return new List<int>() { 1, 2, 3, };
}
does?
>`, the only thing you can add to it is `List`. So if you have a reference R to `List
>` where R is typed `IList>`, the static type checking will allow you to add things that the actual object doesn't. Casting a reference is for when the thing it refers to *actually is* the type you're casting to. In this case, it isn't.