I need some help. It is pretty easy. I have this piece of code, and I would like to discuss if it is correct, or if you suggest a better way to do it. I have an idea about the answer but I would like to see your answers. here it goes
if (myObject is ClassA)
{
var myObjectA = myObject as ClassA;
myObjectA?.MethodJustInA();
}
else if (myObject is ClassB)
{
var myObjectB = myObject as ClassB;
myObjectB?.MethodJustInB();
myObjectB?.OtherMethodJustInB();
}
I think there is no need to create a new object after every if, just doing:
(ClassB)myObjectB.MethodJustInB();
And there is no need to check it is null since if it is within the if is because is not null
Thanks