奇数C#三元运营商行为

发布于 2025-01-24 20:15:27 字数 1534 浏览 3 评论 0原文

因此,我今天在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:

enter image description here

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

内心荒芜 2025-01-31 20:15:27

三元运算符是 - 嗯 - ocerator ,这只是一种特殊的方法。而且,作为任何其他方法,它只能具有一种单一返回类型

您尝试做的是使用操作员返回十进制? a int?,取决于条件。那是不可能的。

会议的是,编译器知道从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 an int? depending on a condition. That is not possible.

What happens is that the compiler knows that there is an implicit conversion from int? to decimal?, but not the other way round. So it infers the return type of the operator as decimal? and implicitly converts your intValue into a decimal?.

咽泪装欢 2025-01-31 20:15:27

三元表达式返回一种类型,而不是评估结果的条件。

您的INT被提升为十进制,以充分满足这一要求。

如果无法应用转换,您将获得编译错误。

first_expression的类型和second_expression必须相同,或者必须从一种类型到另一种类型存在隐式转换。

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.

Either the type of first_expression and second_expression must be the same, or an implicit conversion must exist from one type to the other.

https://msdn.microsoft.com/en-us/library/ty67wk28.aspx

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文