0

Why I can do the following conversion:

List<String> strings = new List<String>();
IEnumerable<Object> iStrings = strings as IEnumerable<Object>;
List<Object> oStrings = iStrings.ToList();

But I can't do it straight:

List<String> strings = new List<String>();
List<Object> oStrings = strings as List<Object>;
TakinosaJi
  • 400
  • 5
  • 13
  • 1
    I do not understand the question. `.ToList()` calls a method. It does not cast or convert other than perhaps in the plain English meaning of the word, which doesn't make sense to me in the rest of your question. Can you elaborate? –  Oct 28 '15 at 17:07
  • @hvd It *does* convert. It doesn't cast. – Servy Oct 28 '15 at 17:22
  • @Servy In the plain English meaning of the word, perhaps. In the C# meaning of the word, it doesn't. And the question suggests to me (possibly incorrectly) the C# meaning of the word. I already covered that in my comment. –  Oct 28 '15 at 17:48
  • @hvd No. This is converting the object, in the C# meaning of the word. Creating a new object that holds content that is conceptually the same as another is what it means to "convert" an object into another type in C#, which is exactly what `ToList` is doing. – Servy Oct 28 '15 at 17:50
  • @Servy In the C# language specification, as far as I have ever seen, "convert" just means implicit type conversions and explicit ones, the explicit ones being casts. I don't know what other possible C# meaning you're thinking of. –  Oct 28 '15 at 17:58
  • @hvd Those are two ways you can convert objects, yes. The fact that you can write an operator to convert an object to another type doesn't mean you can't also write a method to convert an object to another type, or that such a method isn't "converting" the object, because it is. – Servy Oct 28 '15 at 18:02
  • @Servy Again, in the ordinary English meaning of the word, I am not disputing that at all, but where does the C# language specification *ever* use "convert" in that sense? Or what other reference can you give as a better "C# meaning"? All I'm getting from you as an argument so far is "nu-uh!", which isn't particularly productive. –  Oct 28 '15 at 18:03
  • @hvd If the OP had said that `ToList` was performing a user defined implicit conversion, then yes, that would be the wrong term. Saying that it's converting the object is entirely correct. Saying that it doesn't make sense to use the word in this context just isn't true. It'd be like saying that if I claimed that I iterated a list using a `foreach` that it wouldn't be a correct usage of the word "iterate" because it's not in the C# language specs. A term doesn't need to be defined in the language specs to make sense when using it while asking a question about some code. – Servy Oct 28 '15 at 18:09
  • @Servy Now you're responding to an argument I never made. I don't think it's wrong to use the word in the plain English meaning, or any other intelligible meaning, when referring to C# code. I never claimed it would be wrong. –  Oct 28 '15 at 18:16

1 Answers1

2

IEnumerable<T> is covariant, meaning it can be applied to derived types of T. If you're at the zoo looking at an exhibit with several lions, it's perfectly reasonable to also say you're looking at several animals.

List<T> is not covariant, meaning it can not be applied to derived types. It 's not appropriate to call a lion exhibit a collection of animal when you consider adding items. You can't add a gazelle to a lion exhibit or bad things will happen.

When you call ToList<object> on an IEnumerable<object>, you create a new collection with the references typed to object. You can safely add new objects to that list. You can't just "cast" a List<string> to a List<object> because that would imply that you could add other objects besides strings, which is not the case because it is a list of strings.

D Stanley
  • 149,601
  • 11
  • 178
  • 240
  • Thank you for explanation. But in order to allow casting of my custom generic classes ( like Custom ct1 = new Custom(); Custom ct2 = (Custom)ct1 ) i should overload conversion operators in my Custom class? – TakinosaJi Oct 28 '15 at 22:35