224

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

1
  • 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, 2010 at 22:13

9 Answers 9

486

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.

3
  • 9
    A small note: use "is" if you don't want to use the result of the cast and use "as" if you do. Nov 10, 2015 at 14:55
  • 19
    With C# 7 you can combine is and as with pattern matching: if (x is TForm tf) {…
    – Richard
    Nov 24, 2017 at 11:19
  • @AviramFireberger you can not use the time with if ( object is string s) Mar 22, 2021 at 14:24
48
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
}
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:

12

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

if(c is TForm form){
    form.DoStuff();
}
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

5
  • 1
    this is also my personal favorite. typeof(Class).IsAssignableFrom(object.getType()) similar to the Java instanceof operator.
    – SkidRunner
    Nov 7, 2016 at 16:42
  • Does it give false if they are not in the same branch of the inheritance hierarchy but a conversion operator exists? Dec 21, 2017 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. Jan 3, 2018 at 18:55
  • Anyone who has Visual Studio installed to try it? Jan 3, 2018 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. Sep 22, 2019 at 18:12
4

Try the following

if (c is TForm) { 
 ...
}
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.

0

Or

c.getType() == typeOf(TForm)
-1
bool isValid = c.GetType() == typeof(TForm) ? true : false;

or simpler

bool isValid = c.GetType() == typeof(TForm);
1
  • 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 7, 2016 at 16:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.