Search code examples
c#objectidentifier

Identifying the class of an object in a C# IList


I have an IList variable which contains 1 - N entries of myObject. Each entry can be an instance of myObject or any of it's child objects (derived class objects). When doing a foreach over the IList, how can I identify which type of object each member of the IList is?

foreach(myObject anObject in myList)
{
   if(anObject is of type ???)
}

Solution

  • Take a look at the is operator.

    foreach(MyObject anObject in myList)
    {
       if(anObject is MyTypeWhichInheritsFromMyObject)
         ...
    }