Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check if object is NOT of type (!= equivalent for "IS") - C#

This works just fine:

    protected void txtTest_Load(object sender, EventArgs e)     {         if (sender is TextBox) {...}      } 

Is there a way to check if sender is NOT a TextBox, some kind of an equivalent of != for "is"?

Please, don't suggest moving the logic to ELSE{} :)

like image 372
roman m Avatar asked Feb 09 '09 21:02

roman m


People also ask

How do you check if an object is a specific type C#?

To determine whether an object is a specific type, you can use your language's type comparison keyword or construct. For example, you can use the TypeOf… Is construct in Visual Basic or the is keyword in C#. The GetType method is inherited by all types that derive from Object.

How do you check if an object is a certain type of object?

You can check object type in Java by using the instanceof keyword. Determining object type is important if you're processing a collection such as an array that contains more than one type of object. For example, you might have an array with string and integer representations of numbers.

Is object string C#?

A string is an object of type String whose value is text. Internally, the text is stored as a sequential read-only collection of Char objects. There's no null-terminating character at the end of a C# string; therefore a C# string can contain any number of embedded null characters ('\0').

Does C# is check for null?

NULL checks in C# v.We also use NULL propagation method for NULL check. The question mark symbol which used in if condition, which means that it'll check whether value is NULL, if not then it'll check whether Name is null. It'll check if the value is Null, if Null it'll return “value is null” string.


1 Answers

This is one way:

if (!(sender is TextBox)) {...} 
like image 119
Jon Tackabury Avatar answered Sep 29 '22 18:09

Jon Tackabury