I want to check if an object is not of a particular type. I know how to check if something is of a particular type:
if (t is TypeA) { ... }
but
if (t isnt TypeA) { ... }
doesn't work.
The is operator is used to check if the run-time type of an object is compatible with the given type or not. It returns true if the given object is of the same type otherwise, return false.
Method 1: Using the typeof operator The typeof operator returns the type of the variable on which it is called as a string. The return string for any object that does not exist is “undefined”. This can be used to check if an object exists or not, as a non-existing object will always return “undefined”.
UPDATE 2020-10-30:
Times are changing. Starting from C# 9.0 you can use more natural way of checking it:
if(t is not TypeA) { ... }
ORIGINAL ANSWER:
C# is not quite natural language ;) Use this one
if(!(t is TypeA)) { ... }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With