为什么在使用条件运算符时需要显式的ToString()? [英] Why do I need an explicit ToString() when using a conditional operator?
问题描述
我有以下代码来组成一个简单的SOAP Post表单数据值:
I've got the following code to compose a simple SOAP Post form data value:
var postParameters = new Dictionary<string, string>
{
{ "someNumber", "100" },
{ "someString", "Hello World" }
};
var resultWhenNotInConditional =
postParameters
.Keys
.Zip(postParameters.Values,
(key, value) => string.Format("{0}={1}", key, value))
.Aggregate<string, string>(null,
(prev, next) =>
(prev != null)
? string.Format("{0}&{1}", prev, next)
: next);
按设计工作,即
resultWhenNotInConditional = "someNumber=100&someString=Hello World"
但是,当我将其包装在条件运算符中以进行空检查时,就像这样:
However, when I wrap this in a conditional operator to do a null check, like so:
var resultWhenInConditional =
(postParameters != null)
? postParameters
.Keys
.Zip(postParameters.Values,
(key, value) => string.Format("{0}={1}", key, value))
.Aggregate<string, string>(null,
(prev, next) =>
(prev != null)
? string.Format("{0}&{1}", prev, next)
: next)
: string.Empty;
无论postParameters
设置为null还是有效的Dictionary<string, string>
,resultWhenInConditional
似乎始终为空. (将var
更改为显式的string
也无效).
The resultWhenInConditional
seems to be always null, irrespective of whether postParameters
is set to null or to a valid Dictionary<string, string>
. (Changing the var
to an explicit string
also has no effect).
我要解决此问题的唯一方法是在Aggregate
之后添加ToString()
,即:
The only way I can fix this is by adding ToString()
after the Aggregate
, viz:
var resultWhenInConditional =
(postParameters != null)
? postParameters
.Keys
.Zip(postParameters.Values,
(key, value) => string.Format("{0}={1}", key, value))
.Aggregate<string, string>(null,
(prev, next) =>
(prev != null)
? string.Format("{0}&{1}", prev, next)
: next)
.ToString() // R# warns this is redundant
: string.Empty;
所以我的问题是,为什么在条件运算符中需要添加附加的.ToString()
?
So my question is, why do I need to add the additional .ToString()
when inside the conditional operator?
- Microsoft Visual Studio Professional 2010版本10.0.40219.1 SP1Rel
- Microsoft .NET Framework版本4.0.30319 SP1Rel
修改
感谢您的反馈!
要确认这只是一个畸变-VS IDE将变量报告为NULL(在鼠标悬停+立即窗口中), 但仅在使用R#NUnit测试运行器调试单元测试时才进行.在控制台应用程序下进行调试会在IDE中正确报告该值.
To confirm that this is just an aberration - the VS IDE is reporting the variable as NULL (in the mouse hover + Immediate Window), but only when debugging the unit tests individually with the R# NUnit test runner. Debugging under the console App reports the value correctly in the IDE.
即这仅在调试时在Resharper NUnit TestRunner下发生.
i.e. This only happens when debugging, under the Resharper NUnit TestRunner.
通过其他代码(例如Assert
/Console.Writeline
等)访问该变量后,很明显该值实际上并不为空.
As soon as the variable is accessed by further code (e.g. Assert
/ Console.Writeline
etc), it is clear that the value is NOT actually null.
任何单元测试实际上都不会失败,即该值实际上不是null:-)
None of the unit tests actually fail, i.e. the value isn't actually null :-)
- R#7.1.3 C#版(2224)
- NUnit 2.6.1.12217
推荐答案
要确认这只是一个畸变-VS IDE将变量报告为NULL(在鼠标悬停+立即窗口中), 但仅在使用R#NUnit测试运行器调试单元测试时才进行.在控制台应用程序下进行调试会在IDE中正确报告该值.
To confirm that this is just an aberration - the VS IDE is reporting the variable as NULL (in the mouse hover + Immediate Window), but only when debugging the unit tests individually with the R# NUnit test runner. Debugging under the console App reports the value correctly in the IDE.
即这仅在调试时在Resharper NUnit TestRunner下发生.
i.e. This only happens when debugging, under the Resharper NUnit TestRunner.
通过其他代码(例如Assert
/Console.Writeline
等)访问该变量后,很明显该值实际上并不为空.
As soon as the variable is accessed by further code (e.g. Assert
/ Console.Writeline
etc), it is clear that the value is NOT actually null.
任何单元测试实际上都不会失败,即该值实际上不是null:-)
None of the unit tests actually fail, i.e. the value isn't actually null :-)
- R#7.1.3 C#版(2224)
- NUnit 2.6.1.12217
这篇关于为什么在使用条件运算符时需要显式的ToString()?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!