85

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{} :)

nobody
  • 19,814
  • 17
  • 56
  • 77
roman m
  • 26,012
  • 31
  • 101
  • 133

6 Answers6

198

This is one way:

if (!(sender is TextBox)) {...}
Jon Tackabury
  • 47,710
  • 52
  • 130
  • 168
  • 7
    For this particular situation I prefer if (sender is TextBox == false). Less clunky syntax like this. – Igal Tabachnik Feb 09 '09 at 21:16
  • 9
    @hmemcpy: Personally, i cringe whenever i see a comparison to a boolean constant. Probably my C background showing through... Still, makes my skin crawl, and there's no way i'd leave it alone in code i was editing. – Shog9 Feb 09 '09 at 21:17
  • @IgalTabachnik imo if you're going to write logically inverted statements with verbosity for clarity that it becomes more clear to write it as `if (false == sender is TextBox)` – Chris Marisic Oct 06 '16 at 19:13
  • 1
    @Shog9 I started programming a few years ago (and definitely not with C), and I never understood why people use == false or use false ==: it's not more intuitive to me at all and I always prefer ! over it. It's just - redundant. – Mafii Jul 27 '17 at 11:49
  • 1
    After reading the commentary above, I think it is clearer to use `false` rather than negation. The use of the word `is` can be enough visual distraction that a mere glance through the code could easily miss the ! – theMayer May 29 '19 at 18:58
  • any good material or reasons why there is no 'isNot' key word. – Seabizkit Jun 07 '19 at 13:09
  • @Mafii You're forced to if it's a `Nullable` or `bool?`. – Explisam Jul 22 '19 at 06:33
20

C# 9 allows using the not operator. You can just use

if (sender is not TextBox) {...}

instead of

if (!(sender is TextBox)) {...}
Oleh Hrechukh
  • 313
  • 3
  • 9
8

Couldn't you also do the more verbose "old" way, before the is keyword:

if (sender.GetType() != typeof(TextBox)) { // ... }
user247702
  • 23,641
  • 15
  • 110
  • 157
Wayne Molina
  • 19,158
  • 26
  • 98
  • 163
  • 13
    Sure, you could, but do note that the "is" keyword matches any object derived from TextBox, whereas this typeof() check only matches TextBoxes. – mqp Feb 09 '09 at 21:12
3

Two well-known ways of doing it are :

1) Using IS operator:

if (!(sender is TextBox)) {...}

2) Using AS operator (useful if you also need to work with the textBox instance) :

var textBox = sender as TextBox;  
if (sender == null) {...}
John-Philip
  • 3,392
  • 2
  • 23
  • 52
  • For future readers: The difference between `is` and `as` can be found [here](https://learn.microsoft.com/en-gb/dotnet/csharp/language-reference/operators/type-testing-and-cast) – kkuilla Nov 29 '19 at 11:46
-2

If you use inheritance like:

public class BaseClass
{}
public class Foo : BaseClass
{}
public class Bar : BaseClass
{}

... Null resistant

if (obj?.GetType().BaseType != typeof(Bar)) { // ... }

or

if (!(sender is Foo)) { //... }
szydzik
  • 981
  • 8
  • 9
-4

Try this.

var cont= textboxobject as Control;
if(cont.GetType().Name=="TextBox")
{
   MessageBox.show("textboxobject is a textbox");
} 
Joee
  • 1,834
  • 18
  • 19
  • 1
    Why the initial `as Control` line, does it behave differently compared to `GetType()` on an `object`? Other than that, string comparison for something like this is not refactor friendly code. – user247702 Aug 18 '16 at 08:20
  • This is wrong on several levels. There is no need to do the initial "as" if you are going to do the type check by name. Also, don't ever hard code things like "TextBox". If you wanted to do the check that way, do this instead using FullName instead of just Name: if (cont.GetType().FullName == typeof(TextBox).FullName) {}. You don't want to use Name and leave out the namespace in case you create your own custom control in a different namespace and call it TextBox. – rhoonah Jan 13 '23 at 15:15