Is it possible to concat two list that are of different types?
string[] left = { "A", "B", "C" };
int[] right = { 1, 2, 3 };
var result = left.Concat(right);
The code above obciously has a type error. It works if the types match (eg. both are ints or strings).
pom
You can box it.
var result = left.Cast<object>().Concat(right.Cast<object>());
result
will be IEnumerable<object>
.
Then to unbox it, you can use OfType<T>()
.
var myStrings = result.OfType<string>();
var myInts = result.OfType<int>();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With