Dane O'Connor
Dane O'Connor

Reputation: 77298

Whats the difference between RuntimeTypeHandle and Type?

And why would I use one over the other in my code?

Upvotes: 18

Views: 4419

Answers (2)

OmariO
OmariO

Reputation:

In .NET 4.0 Beta 1 RuntimeTypeHandle just wraps RuntimeType. It seems all benefits of using it as a cheap Type proxy have gone.

Evidence for the above claim:

Upvotes: 7

Rytmis
Rytmis

Reputation: 32047

Caution: This answer appears to be out of date. It was posted before .NET 4 became available, which apparently introduced some optimizations regarding Type and thus rendered the information in this answer obsolete. See this more recent answer for details.

According to this blog post (from 2006) by Vance Morrison, RuntimeTypeHandle is a value type (struct) that wraps an unmanaged pointer, so Type.GetTypeHandle(obj).Equals(anotherHandle) is faster to use for strict "is exactly the same type" comparisons than obj.GetType().Equals(anotherType) — the latter creates System.Type instances which are, apparently, heavier.

However, it's also less obvious, and definitely falls under the category "micro-optimization" so if you're wondering when you need one over the other, you should probably just use System.Type.

Upvotes: 12

Related Questions