I have a quick question.
I have a few classes, say Class SubA, SubB and SubC. I also have an abstract class, lets say Parent
So I have an array of Parent objects, which contains instances of SubA, SubB and SubC.
I am basically trying to loop through the array or Parents and get a particular instance of SubA.
I have trieed the following but it produces a type exception:
foreach (SubA a in Parent.GetList())
any help would be greatly appreciated.
 
  
                     
                        
Yes, that current code has an implicit cast, which will fail if you've got an object of the "wrong" type in your collection. I suggest you use LINQ's OfType method:
using System.Linq; // Make LINQ extension methods available
...
foreach (SubA a in Parent.GetList().OfType<SubA>())
{
    ...
}
Note that a will never be null in the above - I'm assuming that's okay.
 
                    See more on this question at Stackoverflow