Checking if an object is null in C#

前端 未结 18 1872
情深已故
情深已故 2020-11-28 02:12

I would like to prevent further processing on an object if it is null.

In the following code I check if the object is null by either:

if (!data.Equal         


        
18条回答
  •  猫巷女王i
    2020-11-28 02:36

    As of C# 9 you can do

    if (obj is null) { ... }
    

    For not null use

    if (obj is not null) { ... }
    

    If you need to override this behaviour use == and != accordingly.

提交回复
热议问题