奇数C#三元运营商行为
因此,我今天在C#中遇到了本地三元运营商的一些令人困惑的行为。三元操作员将错误的数据类型发送到我正在调用的方法。基本的前提是我想将小数值转换为int
如果Decimalentry == false
,以便将其作为int 这是代码:
decimal? _repGroupResult = 85.00
int? intValue = null;
bool decimalEntry = false;
if (decimalEntry == false)
{
intValue = (int?) _repGroupResult;
}
Console.WriteLine("Sending to ResultAdd via ternary operator");
RepGBParent.ResultAdd(this.RepInfo.ResultID, decimalEntry ? _repGroupResult : intValue);
Console.WriteLine("Sending to ResultAdd via if statement");
// All other tests - just add the rep group result
if (decimalEntry)
{
RepGBParent.ResultAdd(this.RepInfo.ResultID, _repGroupResult);
}
else
{
RepGBParent.ResultAdd(this.RepInfo.ResultID, intValue);
}
我正在调用的方法resultadd
在这里:
public void ResultAdd(int pResultID, object pResultValue)
{
if (pResultValue == null) { return; }
Console.WriteLine(this.TestInfo.TestNum + ": " + pResultValue.GetType());
....
}
三元运营商接收DECIMAL
时,如果语句说明发送<代码> int 。如下面的输出代码所示:
我玩了2-3个小时,并找出了在这里发布它的最佳方法,因此我清楚了自己遇到的问题。
请避免“为什么要这样这样做”类型的响应。我只是想知道为什么三元运算符和if语句之间有区别。
我发现与此密切相关的唯一另一篇文章是这个帖子,但并没有完全匹配:
So I came across some very baffling behavior of the native ternary operator in C# today. The ternary operator is sending the wrong data type to the method I'm calling. The basic premise is I want to convert the decimal value to an int
if decimalEntry == false
so it will get stored in a database as an int
Here is the code:
decimal? _repGroupResult = 85.00
int? intValue = null;
bool decimalEntry = false;
if (decimalEntry == false)
{
intValue = (int?) _repGroupResult;
}
Console.WriteLine("Sending to ResultAdd via ternary operator");
RepGBParent.ResultAdd(this.RepInfo.ResultID, decimalEntry ? _repGroupResult : intValue);
Console.WriteLine("Sending to ResultAdd via if statement");
// All other tests - just add the rep group result
if (decimalEntry)
{
RepGBParent.ResultAdd(this.RepInfo.ResultID, _repGroupResult);
}
else
{
RepGBParent.ResultAdd(this.RepInfo.ResultID, intValue);
}
The method I'm calling ResultAdd
is here:
public void ResultAdd(int pResultID, object pResultValue)
{
if (pResultValue == null) { return; }
Console.WriteLine(this.TestInfo.TestNum + ": " + pResultValue.GetType());
....
}
The ternary operator receives a decimal
while the if
statement sends an int
. As shown by the output code below:
I consider myself a programmer of reasonable talent and this really threw me back today. I played around with it for 2-3 hours and figured out the best way to post it here so I'm clear of the issue I'm having.
Please avoid "why are you even doing it that way" type responses. I simply want to know why there is a difference here between the ternary operator and the if statement.
The only other post that I found that was closely related was this one but it didn't quite match up:
Bizarre ternary operator behavior in debugger on x64 platform
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
三元运算符是 - 嗯 - ocerator ,这只是一种特殊的方法。而且,作为任何其他方法,它只能具有一种单一返回类型。
您尝试做的是使用操作员返回
十进制?
或 aint?
,取决于条件。那是不可能的。会议的是,编译器知道从
int?
十进制?有隐式转换,但反过来却不是。因此,它将操作员的返回类型作为十进制?
,并隐式将您的intvalue
转换为DECIMAL?
。The ternary operator is - well - an operator which is only a special kind of method. And as any other method, it can only have one single return type.
What you try to do is to use the operator to return a
decimal?
or anint?
depending on a condition. That is not possible.What happens is that the compiler knows that there is an implicit conversion from
int?
todecimal?
, but not the other way round. So it infers the return type of the operator asdecimal?
and implicitly converts yourintValue
into adecimal?
.三元表达式返回一种类型,而不是评估结果的条件。
您的INT被提升为十进制,以充分满足这一要求。
如果无法应用转换,您将获得编译错误。
https://msdn.microsoft.com/en-en-us/library/library/library/ty67wk28.aspx
The ternary expression returns a single type, not a type conditional on the result of the evaluation.
Your int is promoted to decimal in order to fullful that requirement.
If the conversion could not be applied, you would get a complier error.
https://msdn.microsoft.com/en-us/library/ty67wk28.aspx