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

后端 未结 6 1512
遥遥无期
遥遥无期 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:41

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

提交回复
热议问题