Click here to Skip to main content
11,632,778 members (86,069 online)
Click here to Skip to main content

Tagged as

Getting the one and only Type of an Object

, 7 Oct 2011 CPOL 9.4K 5
Rate this:
Please Sign up or sign in to vote.
How to check an Objects Type, ignoring all subtypes, in C# and VB.
Last week, I had a problem with types and derived types. I wanted to execute some code when a type of SomeButton was clicked. Unfortunately, the piece of code was pretty generic and basically any type of Object could pass my piece of code. The result was that my code did not only execute when SomeButton was clicked, but also when any derived Class of SomeButton was clicked. That was when I learned the subtle difference between instance is Type (C#), TypeOf instance Is Type (VB) and GetType. Below is the code for both C# and VB to check the exact Type of an Object.
This is a Console Application, so you can simply copy/paste the complete example into a new Console Application Project and see the differences for yourself.

C#:
using System;
 
namespace TypeTipCSharp
{
	class Base { }
	class Derived : Base { }
 
	class Program
	{
		static void Main(string[] args)
		{
			Derived d = new Derived();
			Console.WriteLine("Variable d is an instance of Derived, which inherits from Base.");
 
			if (d is Base)
			{ Console.WriteLine("The type of d is Base"); }
			else
			{ Console.WriteLine("The type of d is not Base"); }
 
			if (d.GetType() == typeof(Base))
			{ Console.WriteLine("The GetType of d is Base"); }
			else
			{ Console.WriteLine("The GetType of d is not Base"); }
 
			Base b = new Derived();
			Console.WriteLine("{0}Variable b is of Type Base, but has an instance of Derived assigned to it.", Environment.NewLine);
 
			if (b is Base)
			{ Console.WriteLine("The type of b is Base"); }
			else
			{ Console.WriteLine("The type of b is not Base"); }
 
			if (b.GetType() == typeof(Base))
			{ Console.WriteLine("The GetType of b is Base"); }
			else
			{ Console.WriteLine("The GetType of b is not Base"); }
 
			if (b is Derived)
			{ Console.WriteLine("The type of b is Derived"); }
			else
			{ Console.WriteLine("The type of b is not Derived"); }
 
			if (b.GetType() == typeof(Derived))
			{ Console.WriteLine("The GetType of b is Derived"); }
			else
			{ Console.WriteLine("The GetType of b is not Derived"); }
 
			Console.WriteLine("{0}Press a key to close.", Environment.NewLine);
			Console.ReadKey();
 
		}
	}
}

VB:
Public Class Base
End Class
 
Public Class Derived
	Inherits Base
End Class
 
Module Module1
 
	Sub Main()
		Dim d As New Derived
		Console.WriteLine("Variable d is an instance of Derived, which inherits from Base.")
 
		If TypeOf d Is Base Then
			Console.WriteLine("The type of d is Base")
		Else
			Console.WriteLine("The type of d is not Base")
		End If
 
		If d.GetType = GetType(Base) Then
			Console.WriteLine("The GetType of d is Base")
		Else
			Console.WriteLine("The GetType of d is not Base")
		End If
 
		Dim b As Base = New Derived
		Console.WriteLine("{0}Variable b is of Type Base, but has an instance of Derived assigned to it.", Environment.NewLine)
 
		If TypeOf b Is Base Then
			Console.WriteLine("The type of b is Base")
		Else
			Console.WriteLine("The type of b is not Base")
		End If
 
		If b.GetType = GetType(Base) Then
			Console.WriteLine("The GetType of b is Base")
		Else
			Console.WriteLine("The GetType of b is not Base")
		End If
 
		If TypeOf b Is Derived Then
			Console.WriteLine("The type of b is Derived")
		Else
			Console.WriteLine("The type of b is not Derived")
		End If
 
		If b.GetType = GetType(Derived) Then
			Console.WriteLine("The GetType of b is Derived")
		Else
			Console.WriteLine("The GetType of b is not Derived")
		End If
 
		Console.WriteLine("{0}Press a key to close.", Environment.NewLine)
		Console.ReadKey()
	End Sub
 
End Module

The result:
Variable d is an instance of Derived, which inherits from Base.
The type of d is Base
The GetType of d is not Base
 
Variable b is of Type Base, but has an instance of Derived assigned to it.
The type of b is Base
The GetType of b is not Base
The type of b is Derived
The GetType of b is Derived

So GetType will always return the exact type of an Object which can be compared to a Type. Only when the two types have an exact match do they really match. As you can see, Derived is a type of Base, but Derived.GetType does not match Base.
instance is Type and TypeOf instance Is Type return True whenever the instance is of the type Type or any derived type!
The difference is subtle, but very important. Smile | :)

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Sander Rossel
Software Developer
Netherlands Netherlands

Sander Rossel is a professional developer with a working experience in .NET (VB and C#, WinForms, MVC, Entity Framework), JavaScript and SQL Server.

He has an interest in various technologies inlcuding, but not limited to, Functional Programming, NoSQL and Software Design.

He seeks to educate others on his blog, Sander's bits - Writing the code you need, and on his CodeProject profile.


You may also be interested in...

Comments and Discussions

 
QuestionGood Pin
Member 103795027-Nov-13 5:05
memberMember 103795027-Nov-13 5:05 
AnswerRe: Good Pin
Sander Rossel7-Nov-13 6:20
memberSander Rossel7-Nov-13 6:20 
GeneralRe: Here's another Tip/Trick... You can revote simply by voting ... Pin
Naerling11-Oct-11 22:51
memberNaerling11-Oct-11 22:51 
GeneralRe: Now I feel bad about giving just 3. Pin
Kabwla.Phone11-Oct-11 22:40
memberKabwla.Phone11-Oct-11 22:40 
GeneralReason for my vote of 3 Pretty good information, though I do... Pin
Kabwla.Phone11-Oct-11 22:12
memberKabwla.Phone11-Oct-11 22:12 
GeneralRe: Although I respect your vote of 3 I cannot just let it pass.... Pin
Naerling11-Oct-11 22:37
memberNaerling11-Oct-11 22:37 
GeneralYou could probably mention the Type.IsAssignableFrom method ... Pin
moozzyk10-Oct-11 19:49
membermoozzyk10-Oct-11 19:49 
GeneralReason for my vote of 5 Excellent information Pin
USABebopKid7-Oct-11 9:18
memberUSABebopKid7-Oct-11 9:18 
GeneralRe: Thanks, always good to hear! Pin
Naerling7-Oct-11 12:41
memberNaerling7-Oct-11 12:41 
GeneralReason for my vote of 5 Good explanation of the differences Pin
Reiss6-Oct-11 21:44
memberReiss6-Oct-11 21:44 
GeneralRe: Thanks! Glad you liked it :) Pin
Naerling6-Oct-11 22:04
memberNaerling6-Oct-11 22:04 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.150728.1 | Last Updated 7 Oct 2011
Article Copyright 2011 by Sander Rossel
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid