Check if the object is NOT of type (! = Equivalent to "IS") - C #

This works great:

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

Is there any way to check if the sender is NOT textual, some equivalent! = For "is"?

Please do not suggest moving logic to ELSE {} :)

+47
Feb 09 '09 at 21:04
source share
5 answers

This is one way:

 if (!(sender is TextBox)) {...} 
+126
Feb 09 '09 at 21:06
source share

Could you also make a more detailed "old" way before the is keyword:

 if (sender.GetType() != typeof(TextBox)) { // ... } 
+6
Feb 09 '09 at 21:09
source share

Two well-known ways to do this:

1) Using the IS operator:

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

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

 var textBox = sender as TextBox; if (sender == null) {...} 
+1
Jul 27 '17 at 17:56 on
source share

Try it.

 var cont= textboxobject as Control; if(cont.GetType().Name=="TextBox") { MessageBox.show("textboxobject is a textbox"); } 
-one
Apr 28 '16 at 9:39
source share

If you use type inheritance:

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

... zero stability

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

or

 if (!(sender is Foo)) { //... } 
-one
Jul 27 '17 at 11:17
source share



All Articles