Check if instance is of a type

C#.Net

C# Problem Overview


Using this to check if c is an instance of TForm.

c.GetType().Name.CompareTo("TForm") == 0

Is there a more type safe way to do it besides using a string as a param to CompareTo()?

C# Solutions


Solution 1 - C#

The different answers here have two different meanings.

If you want to check whether an instance is of an exact type then

if (c.GetType() == typeof(TForm))

is the way to go.

If you want to know whether c is an instance of TForm or a subclass then use is/as:

if (c is TForm)

or

TForm form = c as TForm;
if (form != null)

It's worth being clear in your mind about which of these behaviour you actually want.

Solution 2 - C#

if(c is TFrom)
{
   // Do Stuff
}

or if you plan on using c as a TForm, use the following example:

var tForm = c as TForm;
if(tForm != null)
{
   // c is of type TForm
}

The second example only needs to check to see if c is of type TForm once. Whereis if you check if see if c is of type TForm then cast it, the CLR undergoes an extra check. Here is a reference.

Edit: Stolen from Jon Skeet

If you want to make sure c is of TForm and not any class inheriting from TForm, then use

if(c.GetType() == typeof(TForm))
{
   // Do stuff cause c is of type TForm and nothing else
}

Solution 3 - C#

Yes, the "is" keyword:

if (c is TForm)
{
    ...
}

See details on MSDN: http://msdn.microsoft.com/en-us/library/scekt9xw(VS.80).aspx

> Checks if an object is compatible with a given type. For example, it can be determined if an object is compatible with the string type like this:

Solution 4 - C#

Also, somewhat in the same vein

Type.IsAssignableFrom(Type c)

> "True if c and the current Type represent the same type, or if the > current Type is in the inheritance hierarchy of c, or if the current > Type is an interface that c implements, or if c is a generic type > parameter and the current Type represents one of the constraints of c."

From here: http://msdn.microsoft.com/en-us/library/system.type.isassignablefrom.aspx

Solution 5 - C#

A little more compact than the other answers if you want to use c as a TForm:

if(c is TForm form){
    form.DoStuff();
}

Solution 6 - C#

Try the following

if (c is TForm) { 
 ...
}

Solution 7 - C#

As others have mentioned, the "is" keyword. However, if you're going to later cast it to that type, eg.

TForm t = (TForm)c;

Then you should use the "as" keyword.

e.g. TForm t = c as TForm.

Then you can check

if(t != null)
{
 // put TForm specific stuff here
}

Don't combine as with is because it's a duplicate check.

Solution 8 - C#

Or

c.getType() == typeOf(TForm)

Solution 9 - C#

bool isValid = c.GetType() == typeof(TForm) ? true : false;

or simpler

bool isValid = c.GetType() == typeof(TForm);

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionLennieView Question on Stackoverflow
Solution 1 - C#Jon SkeetView Answer on Stackoverflow
Solution 2 - C#PostManView Answer on Stackoverflow
Solution 3 - C#bojView Answer on Stackoverflow
Solution 4 - C#Brad CunninghamView Answer on Stackoverflow
Solution 5 - C#Ali RahmanView Answer on Stackoverflow
Solution 6 - C#JaredParView Answer on Stackoverflow
Solution 7 - C#taylonrView Answer on Stackoverflow
Solution 8 - C#RaynosView Answer on Stackoverflow
Solution 9 - C#GabeView Answer on Stackoverflow