小编jar*_*ler的帖子

奇数C#三元运算符行为

所以我今天在C#中遇到了本地三元运算符的一些非常令人困惑的行为.三元运算符正在向我正在调用的方法发送错误的数据类型.基本前提是我想将十进制值转换为intif 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);
}
Run Code Online (Sandbox Code Playgroud)

我打电话的方法ResultAdd是:

public void ResultAdd(int pResultID, object pResultValue) …
Run Code Online (Sandbox Code Playgroud)

c# ternary-operator

0
推荐指数
1
解决办法
109
查看次数

标签 统计

c# ×1

ternary-operator ×1