在C#.net中对服务结果进行正确的null检查 [英] Proper null check on service result in c# .net

查看:88
本文介绍了在C#.net中对服务结果进行正确的null检查的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

来自Jared Parsons的推文,是c#编译器开发人员。该推文声称我们应将对象用作非空检查。



我的问题是,这到底是为什么?如果我从呼叫服务获得结果,应该运行是对象检查而不是!=空吗? / p>

根据我的理解以及在Microsoft文档 is关键字应用作类型检查



我无法在Microsoft文档中的任何地方找到 is object作为非null检查示例,这就是为什么它涉及到我是否真的是进行null检查的正确方法的原因。

解决方案

Jared Parsons是正确的(很明显),这不是意见。有具体的优势。如果可以,请使用 is object is null 。现在看起来很不寻常,但将来会越来越普遍。






直到C#7, is 仅用于类型检查。不过从C#7开始,它也用于模式匹配。 为空是一个常数模式,当输入恰好为 null 时匹配。 是对象是字符串匹配类型。



为空为对象是可取的,因为有人可以重载等于 == 运算符。例如,两个大小相等的盒子可以被认为是相等的。 x == null 使用类型的相等运算符,并且仅在该运算符说的是真时才返回true。



什么发生错误,或者有人尝试变得平等聪明,这会发生吗?当我们只需要知道该值是否为空时,为什么还要浪费CPU来调用该运算符?



问题运算符重载==,!=,等于显示了问题:



operator ==()中的代码:

 公共类BOX 
{
public double Height {get; set;}
public double Length {get; set;}
public double广度{get; set;}

公共静态布尔运算符==(BOX b1,BOX b2)
{
if((object)b1 == null)
return(object)b2 == null;

返回b1。等于(b2);
}
...

开始于:

 公共静态布尔运算符==(BOX b1,BOX b2)
{
if(b1 == null)
return(b2 == null);

返回b1。等于(b2);
}

糟糕-这是无限递归!每个比较最终都会再次调用 operator == 。如果使用我们自己的代码:

  if(theBoxFromDB == null)... 

我们也将获得无限递归。回答者通过强制转换为 object 来解决此问题,从而强制使用 Object.Equals 进行比较。



如果使用以下方法,我们可以避免这种不幸的情况:

  if(theBoxFromDB为空) ... 

等式运算符本身可以简化。没有其他强制转换,也没有像其他答案那样调用 ReferenceEquals 。 :

 公共静态布尔运算符==(BOX b1,BOX b2)
{
if(b1为null)
返回(b2为null);

返回b1。等于(b2);
}

当我们开始使用完整模式匹配语法时,事情变得更加有趣。在 if(box为null)中,我们唯一知道的是该框为 null



如果我们使用语法是T名称,则会得到一个强类型的非null变量:

 对象框= LoadFromSomewhere(); 
if(box is Box b)
{
var volume = box.Height * box.Width * box.Breadth;
}


There is a tweet from Jared Parsons who is c# compiler developer. The tweet claims that we should use "is object" as non null check.

My question is why exactly is that and if I'm getting result from call to service should I run "is object" check instead of "!= null"?

From my understanding and what I can see in Microsoft documentation "is" keyword should be used as type check.

I wasn't able to find "is object" as non null check example anywhere in Microsoft documentation that's why it concerns me if this is really right way to do null check.

解决方案

Jared Parsons is right (obviously), and this isn't a matter of opinion. There are concrete advantages. Use is object or is null when you can. It looks unusual now, but will become far more common in the future.


Up until C# 7, is was only used in type checking. Starting with C# 7 though, it's also used for pattern matching. is null is a constant pattern that matches when the input is exactly null. is object or is string s match on the type.

is null and is object are preferable because someone can overload the Equals and == operators. For example, two boxes with equal sizes can be considered equal. x==null uses the type's equality operator and will only return true if that operator says it's true.

What happens though if there's a mistake, or if someone tried to get clever with equality? And why should we waste CPU to call that operator when we only need to know if that value is null?

One of the answers to the question Operator overloading ==, !=, Equals shows the problem :

The code in operator ==() :

public class BOX
{
    public double Height{get;set;}
    public double Length{get;set;}
    public double Breadth{get;set;}

    public static bool operator == (BOX b1, BOX b2)
    {
        if ((object)b1 == null)
            return (object)b2 == null;

        return b1.Equals(b2);
    }
    ...

Started as :

public static bool operator == (BOX b1, BOX b2)
{
    if (b1 == null)
        return (b2 == null);

    return b1.Equals(b2);
}

Oops - that's infinite recursion! Each of those comparisons ends up calling operator == again. If our own code used :

if (theBoxFromDB == null) ...

We'd get an infinite recursion too. The answerer fixed this by casting to object, thus forcing a comparison using Object.Equals .

We can avoid such unfortunate situations though if we use :

if (theBoxFromDB is null) ...

The equality operator itself can be simplified this way. No extra casts, no calls to ReferenceEquals the way other answers do. :

public static bool operator == (BOX b1, BOX b2)
{
    if (b1 is null)
        return (b2 is null);

    return b1.Equals(b2);
}

Things get more interesting when we start using the full pattern matching syntax. In if(box is null) the only thing we know is that the box is null.

If we use the syntax is T name though, we get a strongly typed, non-null variable :

object box=LoadFromSomewhere();
if(box is Box b)
{
    var volume=box.Height*box.Width*box.Breadth;
}

这篇关于在C#.net中对服务结果进行正确的null检查的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆