<p>It seems that a List object cannot be stored in a List variable in C#, and can't even be explicitly cast that way.</p>

<pre><code>List&lt;string&gt; sl = new List&lt;string&gt;();
List&lt;object&gt; ol;
ol = sl;
</code></pre>

<p>results in Cannot implicitly convert type <code>System.Collections.Generic.List&lt;string&gt;</code> to <code>System.Collections.Generic.List&lt;object&gt;</code></p>

<p>And then...</p>

<pre><code>List&lt;string&gt; sl = new List&lt;string&gt;();
List&lt;object&gt; ol;
ol = (List&lt;object&gt;)sl;
</code></pre>

<p>results in Cannot convert type <code>System.Collections.Generic.List&lt;string&gt;</code> to <code>System.Collections.Generic.List&lt;object&gt;</code></p>

<p>Of course, you can do it by pulling everything out of the string list and putting it back in one at a time, but it is a rather convoluted solution.</p>

[{'answer_id': 6559, 'author': 'Rex M', 'author_id': 67, 'author_profile': 'https://Stackoverflow.com/users/67', 'pm_score': 4, 'selected': False, 'text': "<p>The reason is that a generic class like <code>List&lt;&gt;</code> is, for most purposes, treated externally as a normal class. e.g. when you say <code>List&lt;string&gt;()</code> the compiler says <code>ListString()</code> (which contains strings). [Technical folk: this is an extremely plain-English-ified version of what's going on]</p>\n\n<p>Consequently, obviously the compiler can't be smart enough to convert a ListString to a ListObject by casting the items of its internal collection.</p>\n\n<p>That's why there's extension methods for IEnumerable like Convert() that allow you to easily supply conversion for the items stored inside a collection, which could be as simple as casting from one to another.</p>\n"}, {'answer_id': 6567, 'author': 'Mike Stone', 'author_id': 122, 'author_profile': 'https://Stackoverflow.com/users/122', 'pm_score': 6, 'selected': True, 'text': "<p>Think of it this way, if you were to do such a cast, and then add an object of type Foo to the list, the list of strings is no longer consistent.  If you were to iterate the first reference, you would get a class cast exception because once you hit the Foo instance, the Foo could not be converted to string!</p>\n\n<p>As a side note, I think it would be more significant whether or not you can do the reverse cast:</p>\n\n<pre><code>List&lt;object&gt; ol = new List&lt;object&gt;();\nList&lt;string&gt; sl;\nsl = (List&lt;string&gt;)ol;\n</code></pre>\n\n<p>I haven't used C# in a while, so I don't know if that is legal, but that sort of cast is actually (potentially) useful.  In this case, you are going from a more general class (object) to a more specific class (string) that extends from the general one.  In this way, if you add to the list of strings, you are not violating the list of objects.</p>\n\n<p>Does anybody know or can test if such a cast is legal in C#?</p>\n"}, {'answer_id': 6569, 'author': 'Matt Sheppard', 'author_id': 797, 'author_profile': 'https://Stackoverflow.com/users/797', 'pm_score': 2, 'selected': False, 'text': '<p>Mike - I believe contravariance isn\'t allowed in C# either</p>\n\n<p>See <a href="http://blogs.msdn.com/rmbyers/archive/2005/02/16/375079.aspx" rel="nofollow noreferrer">Generic type parameter variance in the CLR</a> for some more info.</p>\n'}, {'answer_id': 6574, 'author': 'Jon Limjap', 'author_id': 372, 'author_profile': 'https://Stackoverflow.com/users/372', 'pm_score': 3, 'selected': False, 'text': "<p>This has a lot to do with covariance, e.g., generic types are considered as parameters, and if the parameters do not resolve properly to a more specific type then the operation fails. The implication of such is that you really cannot cast to a more general type like object. And as stated by Rex, the List object won't convert each object for you.</p>\n\n<p>You might want to try the ff code instead:</p>\n\n<pre><code>List&lt;string&gt; sl = new List&lt;string&gt;();\n//populate sl\nList&lt;object&gt; ol = new List&lt;object&gt;(sl);\n</code></pre>\n\n<p>or:</p>\n\n<pre><code>List&lt;object&gt; ol = new List&lt;object&gt;();\nol.AddRange(sl);\n</code></pre>\n\n<p>ol will (theoretically) copy all the contents of sl without problems.</p>\n"}, {'answer_id': 6577, 'author': 'Ray', 'author_id': 233, 'author_profile': 'https://Stackoverflow.com/users/233', 'pm_score': 5, 'selected': False, 'text': "<p>If you're using .NET 3.5 have a look at the Enumerable.Cast method. It's an extension method so you can call it directly on the List.</p>\n\n<pre><code>List&lt;string&gt; sl = new List&lt;string&gt;();\nIEnumerable&lt;object&gt; ol;\nol = sl.Cast&lt;object&gt;();\n</code></pre>\n\n<p>It's not exactly what you asked for but should do the trick.</p>\n\n<p>Edit: As noted by Zooba, you can then call ol.ToList() to get a List</p>\n"}, {'answer_id': 7168, 'author': 'Zooba', 'author_id': 891, 'author_profile': 'https://Stackoverflow.com/users/891', 'pm_score': 4, 'selected': False, 'text': "<p>You cannot cast between generic types with different type parameters. Specialized generic types don't form part of the same inheritance tree and so are unrelated types.</p>\n\n<p>To do this pre-NET 3.5:</p>\n\n<pre><code>List&lt;string&gt; sl = new List&lt;string&gt;();\n// Add strings to sl\n\nList&lt;object&gt; ol = new List&lt;object&gt;();\n\nforeach(string s in sl)\n{\n    ol.Add((object)s);  // The cast is performed implicitly even if omitted\n}\n</code></pre>\n\n<p>Using Linq:</p>\n\n<pre><code>var sl = new List&lt;string&gt;();\n// Add strings to sl\n\nvar ol = new List&lt;object&gt;(sl.Cast&lt;object&gt;());\n\n// OR\nvar ol = sl.Cast&lt;object&gt;().ToList();\n\n// OR (note that the cast to object here is required)\nvar ol = sl.Select(s =&gt; (object)s).ToList();\n</code></pre>\n"}, {'answer_id': 7205, 'author': 'Valters Vingolds', 'author_id': 885, 'author_profile': 'https://Stackoverflow.com/users/885', 'pm_score': 1, 'selected': False, 'text': '<p>That\'s actually so that you don\'t try to put any odd "object" in your "ol" list variant (as <code>List&lt;object&gt;</code> would seem to allow) - because your code would crash then (because the list really is <code>List&lt;string&gt;</code> and will only accept String type objects). That\'s why you can\'t cast your variable to a more general specification.</p>\n\n<p>On Java it\'s the other way around, you don\'t have generics, and instead everything is List of object at runtime, and you really can stuff any strange object in your supposedly-strictly typed List. Search for "Reified generics" to see a wider discussion of java\'s problem...</p>\n'}, {'answer_id': 189354, 'author': 'Constantin', 'author_id': 20310, 'author_profile': 'https://Stackoverflow.com/users/20310', 'pm_score': 1, 'selected': False, 'text': '<p>Such covariance on generics is not supported, but you can actually do this with arrays:</p>\n\n<pre><code>object[] a = new string[] {"spam", "eggs"};\n</code></pre>\n\n<p>C# performs runtime checks to prevent you from putting, say, an <code>int</code> into <code>a</code>.</p>\n'}, {'answer_id': 215536, 'author': 'Tamas Czinege', 'author_id': 8954, 'author_profile': 'https://Stackoverflow.com/users/8954', 'pm_score': 3, 'selected': False, 'text': '<p>Yes, you can, from .NET 3.5:</p>\n\n<pre><code>List&lt;string&gt; sl = new List&lt;string&gt;();\nList&lt;object&gt; ol = sl.Cast&lt;object&gt;().ToList();\n</code></pre>\n'}, {'answer_id': 273207, 'author': 'MattValerio', 'author_id': 35554, 'author_profile': 'https://Stackoverflow.com/users/35554', 'pm_score': 2, 'selected': False, 'text': '<p>I think that this (contravariance) will actually be supported in C# 4.0.\n<a href="http://blogs.msdn.com/charlie/archive/2008/10/27/linq-farm-covariance-and-contravariance-in-visual-studio-2010.aspx" rel="nofollow noreferrer">http://blogs.msdn.com/charlie/archive/2008/10/27/linq-farm-covariance-and-contravariance-in-visual-studio-2010.aspx</a></p>\n'}, {'answer_id': 965792, 'author': 'Alvin Ashcraft', 'author_id': 1137, 'author_profile': 'https://Stackoverflow.com/users/1137', 'pm_score': 0, 'selected': False, 'text': "<p>Here is another pre-.NET 3.5 solution for any IList whose contents can be cast implicitly.</p>\n\n<pre><code>public IList&lt;B&gt; ConvertIList&lt;D, B&gt;(IList&lt;D&gt; list) where D : B\n{\n    List&lt;B&gt; newList = new List&lt;B&gt;();\n\n    foreach (D item in list)\n    {\n        newList.Add(item);\n    }\n\n    return newList;\n}\n</code></pre>\n\n<p>(Based on Zooba's example)</p>\n"}, {'answer_id': 1571309, 'author': 'Menno', 'author_id': 190472, 'author_profile': 'https://Stackoverflow.com/users/190472', 'pm_score': 0, 'selected': False, 'text': '<p>I have a:</p>\n\n<pre><code>private List&lt;Leerling&gt; Leerlingen = new List&lt;Leerling&gt;();\n</code></pre>\n\n<p>And I was going to fill it with data collected in an <code>List&lt;object&gt;</code>\nWhat finally worked for me was this one:</p>\n\n<pre><code>Leerlingen = (List&lt;Leerling&gt;)_DeserialiseerLeerlingen._TeSerialiserenObjecten.Cast&lt;Leerling&gt;();\n</code></pre>\n\n<p><code>.Cast</code> it to the type you want to get an <code>IEnumerable</code> from that type, then typecast the <code>IEnemuerable</code> to the <code>List&lt;&gt;</code> you want.</p>\n'}, {'answer_id': 36072174, 'author': 'Cristinadeveloper', 'author_id': 5875199, 'author_profile': 'https://Stackoverflow.com/users/5875199', 'pm_score': 0, 'selected': False, 'text': '<p>Mm, thanks to previous comments I found two ways to find it out. The first one is getting the string list of elements and then casting it to IEnumerable object list:</p>\n\n<pre><code>IEnumerable&lt;object&gt; ob;\nList&lt;string&gt; st = new List&lt;string&gt;();\nob = st.Cast&lt;object&gt;();\n</code></pre>\n\n<p>And the second one is avoiding the IEnumerable object type, just casting the string to object type and then using the function "toList()" in the same sentence:</p>\n\n<pre><code>List&lt;string&gt; st = new List&lt;string&gt;();\nList&lt;object&gt; ob = st.Cast&lt;object&gt;().ToList();\n</code></pre>\n\n<p>I like more the second way. I hope this helps.</p>\n'}, {'answer_id': 43654389, 'author': 'Kozen', 'author_id': 7146016, 'author_profile': 'https://Stackoverflow.com/users/7146016', 'pm_score': 0, 'selected': False, 'text': '<pre><code>List&lt;string&gt; sl = new List&lt;string&gt;();\nList&lt;object&gt; ol;\nol = new List&lt;object&gt;(sl);\n</code></pre>\n'}]
['https://Stackoverflow.com/questions/6557', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/797/']