If Dog enherits from Animal.
And I have a Animal[], that I happen to know contains only dogs. What's the fastest/best way to get my hands on a Dog[] ? I've used new ArrayList(oldarray).ToArray(typeof(Dog)); so far, but that feels a bit clumsy, and I'm wondering if there is something more elegant.
UPDATE: Using the .net 2.0 profile. Should have offcourse mentioned this straight away. I hope editing the original question adheres to the stackoverflow netiquette in this case. I'm looking forward to the day where we can upgrade and use Linq.
Bye, Lucas
var dog_arr = Array.ConvertAll(animal_arr, x => (Dog) x);
Using LINQ this will be
oldarray.Cast<Dog>().ToArray();
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