Checking if an object is null in C#

前端 未结 18 1863
情深已故
情深已故 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条回答
  •  谎友^
    谎友^ (楼主)
    2020-11-28 02:28

    in C# > 7.0 use

    if (obj is null) ...

    This will ignore any == or != defined by the object (unless of course you want to use them ...)

    For not null use if (obj is object) and from C# 9 you can also use if (obj is not null)

提交回复
热议问题