Peladao
Peladao

Reputation: 4090

Fastest way to see if two objects are of the same type

What is the fastest way to see if to objects are of the same type?

I'm now using obj1.GetType.Equals(obj2.GetType) but I'm hoping there is a faster way.

Upvotes: 0

Views: 711

Answers (2)

Dima
Dima

Reputation: 1761

Well, this way is the fastest :)

But you can write:

If obj1.GetType Is obj2.GetType Then

End If

Upvotes: 4

Richard
Richard

Reputation: 8280

Note, I only know C#, but hopefully the idea will help

Alternatively you could do:

obj1.GetType() == obj2.GetType()

Additionally, if you find yourself using this a lot, you could make an extension method. Like so

public static bool IsSameTypeAs(this object source, object comparator)
{
    return source.GetType().Equals(comparator.GetType();
}

// usage
obj1.IsSameTypeAs(obj2)

Upvotes: 0

Related Questions