In C#, why can't a List object be stor

2018-12-31 05:30发布

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

List<string> sl = new List<string>();
List<object> ol;
ol = sl;

results in Cannot implicitly convert type System.Collections.Generic.List<string> to System.Collections.Generic.List<object>

And then...

List<string> sl = new List<string>();
List<object> ol;
ol = (List<object>)sl;

results in Cannot convert type System.Collections.Generic.List<string> to System.Collections.Generic.List<object>

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.

14条回答
伤终究还是伤i
2楼-- · 2018-12-31 06:19

I have a:

private List<Leerling> Leerlingen = new List<Leerling>();

And I was going to fill it with data collected in an List<object> What finally worked for me was this one:

Leerlingen = (List<Leerling>)_DeserialiseerLeerlingen._TeSerialiserenObjecten.Cast<Leerling>();

.Cast it to the type you want to get an IEnumerable from that type, then typecast the IEnemuerable to the List<> you want.

查看更多
伤终究还是伤i
3楼-- · 2018-12-31 06:23

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.

List<string> sl = new List<string>();
IEnumerable<object> ol;
ol = sl.Cast<object>();

It's not exactly what you asked for but should do the trick.

Edit: As noted by Zooba, you can then call ol.ToList() to get a List

查看更多
永恒的永恒
4楼-- · 2018-12-31 06:23

That's actually so that you don't try to put any odd "object" in your "ol" list variant (as List<object> would seem to allow) - because your code would crash then (because the list really is List<string> and will only accept String type objects). That's why you can't cast your variable to a more general specification.

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...

查看更多
残风、尘缘若梦
5楼-- · 2018-12-31 06:25

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:

IEnumerable<object> ob;
List<string> st = new List<string>();
ob = st.Cast<object>();

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:

List<string> st = new List<string>();
List<object> ob = st.Cast<object>().ToList();

I like more the second way. I hope this helps.

查看更多
孤独寂梦人
6楼-- · 2018-12-31 06:28

Such covariance on generics is not supported, but you can actually do this with arrays:

object[] a = new string[] {"spam", "eggs"};

C# performs runtime checks to prevent you from putting, say, an int into a.

查看更多
回忆,回不去的记忆
7楼-- · 2018-12-31 06:28
List<string> sl = new List<string>();
List<object> ol;
ol = new List<object>(sl);
查看更多
登录 后发表回答