How to cast Generic Lists dynamically in C#?

.net, c#, casting, generics, list

Solution

If you can use LINQ then the `Cast` method will do what you need:

List<string> listString = listObject.Cast<string>().ToList();

You can also use the `ConvertAll` method, as Stan points out in his answer:

List<string> listString = listObject.ConvertAll(x => (string)x);

If you're not using C#3 then you'll need to use the "old" delegate syntax rather than a lambda:

List<string> listString =
    listObject.ConvertAll(delegate(object x) { return (string)x; });

Problem

I'm trying to cast `List<object>` to `List<string>` dynamically. I've tried several ways, but I can't find a solution. This is a small sample that shows the problem: ``` List<object> listObject = new List<object>(); listObject.Add("ITEM 1"); listObject.Add("ITEM 2"); listObject.Add("ITEM 3"); List<string> listString = ¿¿listObject??; ``` Thanks in advance!

Original source