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

后端 未结 6 1506
遥遥无期
遥遥无期 2020-12-29 19:16

This works just fine:

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

    }

Is ther

6条回答
  •  轮回少年
    2020-12-29 19:51

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

提交回复
热议问题