Comparing object types

K

Kenneth Baltrinic

What is the best way to determine if two objects are of the same type? The
'is' operator is not precise enough. Currently I am using
Obj1.GetType().Name == Obj2.GetType().Name but it seems to me there must be
a better way. The following code gives a better idea of the situation:

abstract class A
{
//the arguments could be instances of any of the classes defined
static void TestFunc(A obj1, A obj2)
{
if(obj1.GetType().Name == obj2.GetType().Name)
{ ... //some code}
else
{ ... // do something else }
}
}
class B {} : A
class B2 {} : A
class C {} : B

Just a thought: would (Obj1 is Obj2 && Obj2 is Obj1) work better and be more
efficient?

--Ken
 
F

Frank Oquendo

Kenneth said:
Just a thought: would (Obj1 is Obj2 && Obj2 is Obj1) work better and
be more efficient?

I'm not following you. Why two tests? If either is true so is the other.

--
There are 10 kinds of people. Those who understand binary and those who
don't.

http://code.acadx.com
(Pull the pin to reply)
 
1

100

Hi Kenneth,
Obj1.GetType().Name == Obj2.GetType().Name but it seems to me there must
be

Obj1.GetType() == Obj2GetType()

There is only one Type-class instance per type. Thus, if the objects are of
the same type GetType method will return the same reference regardless of
the object which GetType method is called.

HTH
B\rgds
100
 
C

cs

Kenneth Baltrinic said:
What is the best way to determine if two objects are of the same type? The
'is' operator is not precise enough. Currently I am using
Obj1.GetType().Name == Obj2.GetType().Name but it seems to me there must be
a better way. The following code gives a better idea of the situation:

abstract class A
{
//the arguments could be instances of any of the classes defined
static void TestFunc(A obj1, A obj2)
{
if(obj1.GetType().Name == obj2.GetType().Name)
{ ... //some code}
else
{ ... // do something else }
}
}
class B {} : A
class B2 {} : A
class C {} : B

Just a thought: would (Obj1 is Obj2 && Obj2 is Obj1) work better and be more
efficient?

--Ken

in java there is only one object Class created for each different class, in
C# my guess is that there is only one Type created for each different type,
so calling .Equals on a type should always give you the correct answer, for
example this prints true:
Console.WriteLine("asd".GetType().Equals("asd1222".GetType()));
 
K

Kenneth Baltrinic

Ah that was exactly what I was hoping was the case but could not confirm.
Glad to know it.
 
J

Jon Skeet [C# MVP]

cs said:
in java there is only one object Class created for each different class, in
C# my guess is that there is only one Type created for each different type,
so calling .Equals on a type should always give you the correct answer, for
example this prints true:
Console.WriteLine("asd".GetType().Equals("asd1222".GetType()));

There only being one Type instance created for each type means that you
*don't* need to call Equals - you can just use ==.

If you ended up with multiple Type instances for each type, *then* you
would probably need to use Equals (which would probably do the right
thing).
 
C

cs

Jon Skeet said:
There only being one Type instance created for each type means that you
*don't* need to call Equals - you can just use ==.

If you ended up with multiple Type instances for each type, *then* you
would probably need to use Equals (which would probably do the right
thing).

true :)
 
A

Abdessamad Belangour

Hi,
As we're never sure that two objects have the same reference even with the
same data and to avoid confusion, the comparison must take into account the
namespace and the assembly of the compared types.
The function i personnaly use is :

public bool areEqualTypes(Type type1,Type type2)
{
bool equal=false;
if
((type1.Assembly.FullName==type2.Assembly.FullName)&&(type1.Name==type2.Name
)&&(type1.Namespace==type2.Namespace))
equal=true;
return equal;
}

Hope this helps.
Abdessamad.
 
J

Jon Skeet [C# MVP]

Abdessamad Belangour said:
As we're never sure that two objects have the same reference even with the
same data

Are we not? Not *in general*, admittedly, but from the documentation of
Type:

<quote>
A Type object that represents a type is unique; that is, two Type
object references refer to the same object if and only if they
represent the same type. This allows for comparison of Type objects
using reference equality.
</quote>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top