229

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()?

Alexander Abakumov
  • 13,617
  • 16
  • 88
  • 129
Lennie
  • 3,197
  • 4
  • 24
  • 26
  • 24
    I certainly hope you don't do it in Java that way either. Java's `instanceof` and C#'s `is` are much better ways of doing it. – Powerlord Aug 24 '10 at 22:13

9 Answers9

495

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.

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
50
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
}
PostMan
  • 6,899
  • 1
  • 43
  • 51
15

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:

boj
  • 10,935
  • 5
  • 38
  • 57
13

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

if(c is TForm form){
    form.DoStuff();
}
Ali Rahman
  • 141
  • 1
  • 5
12

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

Sean Allred
  • 3,558
  • 3
  • 32
  • 71
Brad Cunningham
  • 6,402
  • 1
  • 32
  • 39
  • 1
    this is also my personal favorite. `typeof(Class).IsAssignableFrom(object.getType())` similar to the Java `instanceof` operator. – SkidRunner Nov 07 '16 at 16:42
  • Does it give false if they are not in the same branch of the inheritance hierarchy but a conversion operator exists? – Paul Stelian Dec 21 '17 at 23:57
  • Good question @PaulStelian. I am not sure off the top of my head but my guess would be it would return a false in that situation. That would at least be my expected behavior. Possibly if an implicit conversion exists it might return true but that would be odd. – Brad Cunningham Jan 03 '18 at 18:55
  • Anyone who has Visual Studio installed to try it? – Paul Stelian Jan 03 '18 at 19:41
  • 1
    @PaulStelian - it returns false. This can be seen by following the doc link, and observing there is no mention of conversions. Another way to think about it is that `T1.IsAssignableFrom(T2)` returns `true` in situations where the `as` operator returns a non-null value, given instances of those types. – ToolmakerSteve Sep 22 '19 at 18:12
4

Try the following

if (c is TForm) { 
 ...
}
JaredPar
  • 733,204
  • 149
  • 1,241
  • 1,454
2

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.

taylonr
  • 10,732
  • 5
  • 37
  • 66
0

Or

c.getType() == typeOf(TForm)
Raynos
  • 166,823
  • 56
  • 351
  • 396
-1
bool isValid = c.GetType() == typeof(TForm) ? true : false;

or simpler

bool isValid = c.GetType() == typeof(TForm);
Gabe
  • 49,577
  • 28
  • 142
  • 181
  • IMHO: I would avoid a direct compassion (ie. `==`). In object or oriented languages supporting inheritance unless you know that your specific Type will never be inherited from for instance a `sealed` Class. Also: use of a ternary operator returning (static/constant) boolean values bothers me, I would be less bothered if it was a `switch` statement. – SkidRunner Nov 07 '16 at 16:52