了解空合并运算符(??) [英] Understanding the null coalescing operator (??)

查看:182
本文介绍了了解空合并运算符(??)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个自定义的的WebControl 它实现了一个 .value的的getter / setter返回可空<十进制>

I have a custom WebControl which implements a .Value getter/setter returning a Nullable<decimal>

这是一个客户端过滤文本框(文本框的一个子类与包含的JavaScript和设置一些服务器端逻辑/获取值)

It's a client-side filtered textbox (a subclass of TextBox with included javascript and some server side logic for setting/getting the value)

下面是吸气和放大器;从控制二传手:

Here is the getter & the setter from that control:

public decimal? Value
{
    get
    {
        decimal amount = 0;
        if (!decimal.TryParse(this.Text, NumberStyles.Currency, null, out amount))
        {
            return null;
        }
        else
        {
            return amount;
        }
    }
    set
    {
        if (!value.HasValue)
        {
            this.Text = "";
        }
        else
        {
            this.Text = string.Format("${0:#,##0.00}", value);
        }
    }
}

这是我的问题看到的是,从这个语句的输出:

The problem that I'm seeing is that the output from this statement:

decimal Amount = uxAmount.Value ?? 0M;



我看到的量被设定为0时, uxAmount.Value 返回1​​0000

I am seeing Amount being set to "0" when uxAmount.Value returns 10000.

这工作如我所料(请原谅在外壳的变化):

This worked as I expected (excuse the change in casing):

decimal? _Amount = uxAmount.Value;
decimal amount = _Amount ?? 0;



我也看到了这种行为(最近)呼吁沿着一个LINQ2SQL数据上下文定义的UDF函数时与空合并运算符,这是我知道我的UDF调用返回的预期值,但我得到的RHS值来代替。

I have also seen this behaviour (recently) when calling a UDF function defined on a Linq2Sql data context along with the null coalescing operator, that is I knew my UDF call returned the expected value but I was getting the RHS value instead.

另外困惑我,如果我评价uxAmount。在手表的价值,我得到的类型的10000 可空<小数方式>

Further confusing me, if I evaluate uxAmount.Value in the watch, I get 10000 of type Nullable<decimal>.

下面是一些表情我已经尝试:

Here are some expressions I've tried:

decimal? _Amount = uxAmount.Value; //10000
decimal amount = _Amount ?? 0; //10000
decimal amount2 = _Amount ?? 0M; //10000
decimal Amount = uxAmount.Value ?? 0M; //0



然后我说这个表达式遵循上述4

Then I added this expression following the above 4

decimal amount3 = (uxTaxAmount.Value) ?? 0M;

现在

decimal Amount = uxAmount.Value ?? 0M; //10000
decimal amount3 = (uxAmount.Value) ?? 0M; //0



这似乎是最后一次调用始终为0,但价值 uxAmount.Value (这是解析出。文本的按上面的getter / setter使用的TryParse 是稳定的。我停在一个断点,并有可能操纵这个值没有其他线程。

It seems like the last call is always 0, but the value of uxAmount.Value (which is parsed out of .Text as per above getter/setter using a TryParse is stable. I'm stopped at a breakpoint and there's no other threads that could manipulate this value.

请注意使用m后缀的​​给力恒成十进制,因为它是整数,我怀疑一个类型转换的问题。

Note the use of the M suffix to force the constant to decimal as it was integer and I suspected a type conversion issue.

任何想法?

。既在LHS及RHS的值似乎是稳定和公知的

The value of both the LHS and RHS appear to be stable and known.

- edit--从VS2010

--edit-- some screengrabs from VS2010

推荐答案

(这个答案是从我上面的意见构成。)

(This answer was constructed from my comments above.)

您保证调试器dsiplays这个正确给你?您是否尝试过一些踩着线进一步向下确保您有 AMOUNT3

Are you sure the debugger dsiplays this correctly to you? Have you tried stepping some lines further down to make sure you have the updated value of amount3?

我的更新值相信这只是与调试器的问题。有时你必须进一步加强了一点。也许翻译的代码(IL)有一定的优化,​​混淆调试器(或什么,我会知道)。但是,如果没有调试器,该值将被什么时候你希望它更新。

I'm sure it's just an issue with the debugger. Sometimes you have to step a little further. Maybe the translated code (IL) has some optimizations that confuse the debugger (or what would I know). But without the debugger, the value will be updated exactly when you expect it.

我见过的其他有经验的开发正在由类似的情况感到困惑,所以我知道有时调试器是一行代码在赋值找一个局部变量的时候落后。也许有人可以找到一个链接讨论呢?

I've seen other experienced developers being confused by similar situations, so I know the debugger sometimes is "one line of code" behind when looking at an assignment to a local variable. Maybe someone can find a link discussing that?

这篇关于了解空合并运算符(??)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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