jaredbaszler
jaredbaszler

Reputation: 4839

Odd C# Ternary Operator Behavior

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

Upvotes: 0

Views: 175

Answers (2)

René Vogt
René Vogt

Reputation: 43946

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?.

Upvotes: 8

Eric J.
Eric J.

Reputation: 150238

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

Upvotes: 4

Related Questions