Using a base class that’s inherited into other classes, sometimes requires that we check which class is the one being used to execute some code.
This will be the base class
public class BaseClass{ }
And this will be the derived classes
public class SubClass1 : BaseClass{ } public class SubClass2 : BaseClass{ }
To check which derived class is being in use we just need to do the following
if (arg is SubClass1 data) { // Do stuff with data which is typed as SubClass1 }
but also we can do the same using a switch statement with pattern matching
switch (arg) { SubClass1 sub1: DoStuff1(sub1); break; SubClass2 sub2: DoStuff2(sub2); break; }
COMMENTS