-2

I have two lists.

List<string> listString;
List<int> listInt;

The input I get in is

List<object>

At runtime, is there a way to convert

List<object> 

to another List?

Edit: My question is how do I it at runtime. Not compile time.

user3587180
  • 1,317
  • 11
  • 23
  • 2
    Yes there is, but it of course depends on what exactly you want to do. What does your input contain? strings? binary data? ints? I mean, _how_ do you want to convert the `object` into an `int` or `string`? – René Vogt May 15 '19 at 17:17
  • Possible duplicate of [Downcasting a list of objects in C#](https://stackoverflow.com/questions/5234111/downcasting-a-list-of-objects-in-c-sharp) – Lesiak May 15 '19 at 17:24

2 Answers2

1
List<string> listString = listObject.Cast<string>().ToList();
List<int> listInt = listObject.Cast<int>().ToList();
André Sanson
  • 440
  • 3
  • 11
0

Try using the .OfType() filter to get the specific lists.

var all = new List<object>();
// add stuff

List<string> text = all.OfType<string>().ToList();
List<int> vals = all.OfType<int>().ToList();
John Alexiou
  • 28,472
  • 11
  • 77
  • 133