C# Polymorphic object comparison
-
Hi, Let's say there are 3 objects: Class A Class B : A Class C : B I want to pass through all objects that derive from Type B C obj1 = new C(); B obj2 = new B(); if(obj1.GetType() == typeof(B)) { //--Pass through all objects that derive from object B or object A... } typeof comapares directly the top types C must equal C... No polymorphic comarison... How to do a polymorphic comparison? Which keyword or mechanism to use? Thanks!
-
Hi, Let's say there are 3 objects: Class A Class B : A Class C : B I want to pass through all objects that derive from Type B C obj1 = new C(); B obj2 = new B(); if(obj1.GetType() == typeof(B)) { //--Pass through all objects that derive from object B or object A... } typeof comapares directly the top types C must equal C... No polymorphic comarison... How to do a polymorphic comparison? Which keyword or mechanism to use? Thanks!
if(obj1 is B)
should work for you -
Hi, Let's say there are 3 objects: Class A Class B : A Class C : B I want to pass through all objects that derive from Type B C obj1 = new C(); B obj2 = new B(); if(obj1.GetType() == typeof(B)) { //--Pass through all objects that derive from object B or object A... } typeof comapares directly the top types C must equal C... No polymorphic comarison... How to do a polymorphic comparison? Which keyword or mechanism to use? Thanks!
As lukasz_nowakowski says,
is
is what you want. The alternative isas
if (obj is B)
{
B bInstance = (B) obj;
...
}or
B bInstance = obj as B;
if (bInstance != null)
{
...
}You can use either, I prefer the second as I prefer explicit null tests. That's just personal preference though.
Did you know: That by counting the rings on a tree trunk, you can tell how many other trees it has slept with.
-
As lukasz_nowakowski says,
is
is what you want. The alternative isas
if (obj is B)
{
B bInstance = (B) obj;
...
}or
B bInstance = obj as B;
if (bInstance != null)
{
...
}You can use either, I prefer the second as I prefer explicit null tests. That's just personal preference though.
Did you know: That by counting the rings on a tree trunk, you can tell how many other trees it has slept with.
-
if(obj1 is B)
should work for youThank you!!
-
The work with polymorphic objects after comparison is very clear to me. I just didn't know how to compare them in C#... Thanks!
-
Hi, Let's say there are 3 objects: Class A Class B : A Class C : B I want to pass through all objects that derive from Type B C obj1 = new C(); B obj2 = new B(); if(obj1.GetType() == typeof(B)) { //--Pass through all objects that derive from object B or object A... } typeof comapares directly the top types C must equal C... No polymorphic comarison... How to do a polymorphic comparison? Which keyword or mechanism to use? Thanks!
FYI - while the use of
is
andas
are what's appropriate for your problem, as lukasz_nowakowski and OriginalGriff have said, your example code used GetType ... which put me in mind of how to do a similar test on Types, rather than objects:Type typeOfC = typeof(C); Type typeofSomeObject = someObject.GetType(); // ... if (typeOfC.IsAssignableFrom(typeofSomeObject)) { // ... } // ...