0

Here's my function signature:

public IList<IList<int>> LevelOrder(TreeNode root)

Here's the snippet showing how I create the IList<IList<int>> that I return from the function:

IList<IList<int>> result = new List<List<int>>();
        result.Add(new List<int>());
        while (completeQueue.Count != 0)
        {
            current = completeQueue.Dequeue();
            if (level != current.level)
            {
                result.Add(new List<int>());
                level = current.level;
            }
            result[level].Add(current.node.val);
        }

        return result;

When I run this function I get a error from the final line (return result): Line 49: Char 36: error CS0266: Cannot implicitly convert type 'System.Collections.Generic.List<System.Collections.Generic.List<int>>' to 'System.Collections.Generic.IList<System.Collections.Generic.IList<int>>'. An explicit conversion exists (are you missing a cast?) (in Solution.cs)

List implements the IList interface. Why doesn't this work?

Adam
  • 8,752
  • 12
  • 54
  • 96
  • 4
    You need `new List>()` – juharr Sep 26 '19 at 15:59
  • Not really a duplicate of [Convert List to List](https://stackoverflow.com/questions/1817300/convert-listderivedclass-to-listbaseclass)... just a miskate of instance type definition. –  Sep 26 '19 at 16:06
  • It is clear from the error that you have not posted the actual code you're using. If it were true that both the return type of the method and the variable `result` were declared as `IList>`, you would not be getting the error you're getting. If `return result;` causes the error you describe, then the `result` variable is obviously declared as `List>` instead. Given that, the problem is exactly as described in the marked duplicate. – Peter Duniho Sep 26 '19 at 16:07
  • @OlivierRogier: it's not clear what you mean by _"instance type definition"_, but...the problem is not simply a matter of the type used in a variable declaration. Even if the OP changes the declaration of `result` so that the type is `IList>` as they've posted above, the code will still fail, but instead on the initialization of the variable instead of on the `return` statement. The problem is that they are attempting a fundamentally illegal conversion. ... – Peter Duniho Sep 26 '19 at 16:12
  • ... With the variable declaration they have now (which is **not** what they've posted above), simply changing the initializer expression won't fix the problem. They need to fix the variable declaration's type too. Most importantly though, to understand why the code they tried to write isn't legal, the marked duplicate is exactly the answer they need. There are a variety of ways to deal with such errors, described in the many answers to that question. But the point is, with the knowledge of why their code doesn't work, the OP can then choose from the available options as they see fit. – Peter Duniho Sep 26 '19 at 16:13
  • 1
    @PeterDuniho I called *instance type definition*, the declared type of the instance. Sorry for my poor english. And it seems to me that using `new List>();` solve the problem as the returned result is implicitally casted to `IList>`. So it is not a duplicate and the link you provide can't help the OP while confusing him because he have a **type mismatch**, not a *base class conversion* problem. –  Sep 26 '19 at 16:34
  • @OlivierRogier: _"the link you provide can't help the OP"_ -- to the contrary, the marked duplicate is exactly what the OP needs. Simply providing them with a different syntax that just happens to make the error go away is not genuine help, because it adds zero comprehension to the problem. Bottom line: the code the OP posted is obviously not their actual code, and the error message they are getting is _exactly_ caused by the issue described and solved in the marked duplicate. – Peter Duniho Sep 26 '19 at 16:48
  • @PeterDuniho I disagree with you and I don't see that the OP question has been edited. –  Sep 26 '19 at 16:54
  • @PeterDuniho the function signature isn't mine, the code snippet is. It's for a LeetCode question. I'm given the signature and I have to fill in the function body – Adam Sep 26 '19 at 16:54
  • @OlivierRogier: the error reads (in part) `Cannot implicitly convert type 'System.Collections.Generic.List>'...`. It would not say that if, as the code posted claims, the variable were declared as `IList> result = new List>();` Indeed, if the actual code were as the OP claims, the error message would occur on that variable declaration, not the `return` statement as they claim. ... – Peter Duniho Sep 26 '19 at 17:01
  • ... Beyond that, and much more to the point, the only _question_ in the post is this one: _"List implements the IList interface. Why doesn't this work?"_ That is **exactly** what the marked duplicate is all about explainining. – Peter Duniho Sep 26 '19 at 17:01

1 Answers1

0

You return a IList<List<int>> instead of a IList<IList<int>>.

This is not the same type because your List<List<int>> implements IList<List<int>>, not IList<IList<int>>.

You should use:

var result = new List<IList<int>>();