Checking if an object is null in C#

前端 未结 18 1866
情深已故
情深已故 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:35

    C# 6 has monadic null checking :)

    before:

    if (points != null) {
        var next = points.FirstOrDefault();
        if (next != null && next.X != null) return next.X;
    }   
    return -1;
    

    after:

    var bestValue = points?.FirstOrDefault()?.X ?? -1;
    

提交回复
热议问题