Answers for "how to cast object to list string in c#"

C#
0

list cast

There are many ways to do that, I think there are two you should consider:

You can use Cast and ToList, but it will require using System.Linq.

var listOfStrings = new List<string>() { "foo", "bar" };
var listOfObjects = listOfStrings.Cast<object>().ToList();

To avoid that, you can use new List<T>(IEnumerable<T> source) constructor. Because IEnumerable<T> is covariant, you can do following:

var listOfStrings = new List<string>() { "foo", "bar" };
var listOfObjects = new List<object>(listOfString);
Posted by: Guest on January-01-1970

C# Answers by Framework

Browse Popular Code Answers by Language