本文介绍了这是Visual Studio 2010编译器中的错误吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

DateTime? date = null;
string tmp = "a" + "(" + date ?? "blablabla" + ")";

Console.WriteLine(tmp);

这将打印接近以下内容:'a('.

This will print something close to: 'a ('.

这是 null-coalescing运算符的错误吗?如果我输入 date?括号中的"blablabla" ,下划线标为错误.

Is this a bug with null-coalescing operator?If I put date ?? "blablabla" in parenthesis, it is underlined as error.

推荐答案

首先,您应该 始终假定这是的错误,而不是编译器的错误; select 不变.您是否诚实地认为 ?? 运算符的Visual Studio 2010实现尚未经过实战测试?当您遇到与您的期望不符的事情时,请检查您的期望.找出手册,并确保完全理解 会发生什么.在这种情况下,请打开语言规范.

First, you should always assume it's your fault, not the compiler's fault; select isn't broken. Do you honestly think the Visual Studio 2010 implementation of the ?? operator hasn't been battle tested? When you encounter something that doesn't match your expectations, check your expectations. Get out the manual, and make sure that you understand exactly what is suppose to happen. In this case, open the language specification.

如果您遵循规范的§1.4,您会看到一个表格,将运算符分为优先级分组;您还可以在线找到它.尤其是,空合并运算符 ?? bottom 附近,仅在条件较低的三元运算符和赋值和 => 之上.以下.因此,您的声明

If you proceed to §1.4 of the specification, you'll see a table that groups operators into precedence groupings; you can also find it online. In particular, the null coalescing operator ?? is near the bottom, above only the lowly conditional ternary operator and assignments and =>. It is below the additive operator. Thus, your statement

string tmp = "a" + "(" + date ?? "blablabla" + ")";

被编译器视为

string tmp = (("a" + "(" + date) ?? ("blablabla" + ")"));

我不会完全学究,并且用括号括起第一个加法表达式.由于该语句中表达式的左侧为 never null,因此,它始终当然会分配"a(" (或 date.HasValue 为true时,将code>"a(" + date.ToString())更改为 tmp .

I'm not going to be completely pedantic and also parenthesize the first additive expression. Since the left-hand-side of the expression in that statement is never null, of course it always assigns "a(" (or "a(" + date.ToString() when date.HasValue is true) to tmp.

主要要点是, you you 应该已经根据手册进行了验证应该有什么错误的期望.

The main point is that you had an incorrect expectation as to what should be happening that you should have verified against the manual.

当然可以.您是否还阅读了错误消息?它可能告诉您不能在 DateTime? string 上执行 ?? ,因为在之间没有隐式转换DateTime? string 沿任一方向.语言规范也涵盖了这一点.参见第7.13节.您必须阅读此消息并对其回复.要语义上等同于您尝试所表达的内容,您必须求助于条件三元运算符:

Of course it is. Did you even read the error message? It probably tells you that you can't do ?? on a DateTime? and a string because there are no implicit conversions between DateTime? and string in either direction. This, too, is covered in the language specification; see §7.13. You have to read this message and respond to it. To get something semantically equivalent to what you're trying to express, you'll have to resort to the conditional ternary operator:

date.HasValue ? date.ToString() : "blablabla"

,然后将整个内容包装在括号中,因为条件三元运算符的优先级非常低.

and then wrap that whole thing in parentheses because the conditional ternary operator has very low precedence.

最后,我发现代码的正确括号括起来很丑陋,阅读起来并不有趣,并且维护起来可能很不愉快.简单说一下,请:

Finally, I find a correctly parenthesized version of your code rather ugly, not fun to read, and probably not enjoyable to maintain. Just make it simple, please:

var tmp = String.Format("a({0})",
                       date.HasValue ? date.ToString() : "blablabla");

现在,如此清除正在发生的事情以及将要发生的事情.我不必思考来了解它.将您的想法保存在遇到的困难问题上.

Now it is so clear what is going on and what is going to happen. I don't have to think to understand it. Save your thinking for the difficult problems you'll encounter.

:小心点.在尝试正确找出首先评估的内容之前,我们需要添加对 date.ToString 的方法调用(优先级最高).

: Be careful. We would need to add in a method call to date.ToString (which has the highest precedence) before attempting to correctly figure out what is evaluated first.

这篇关于这是Visual Studio 2010编译器中的错误吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-29 07:46