Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check if an object is not of a particular type?

Tags:

c#

c#-4.0

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.

like image 227
Sachin Kainth Avatar asked May 11 '12 10:05

Sachin Kainth


People also ask

Which of the following operator determines whether an object is of a certain type in C#?

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.

How do you check it is object or not in JavaScript?

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”.


1 Answers

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)) {    ... } 
like image 133
ie. Avatar answered Oct 09 '22 12:10

ie.