Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why the following Conditional Operator works strangely in StringBuilder containing Nullable type? in C#?

StringBuilder htmlResp=new StringBuilder();
int? cuID= 1;
string cuName="Tom";
string cuEmpID="ZXCV";

htmlResp .Append( "<option value=\"" + cuID.Value + "\">" + cuName+" ("+cuEmpID==""? "-":cuEmpID+")"+  "</option>");
html.Resp.ToString();

I cannot understand why the above code(modified from the actual codes) always gives me weird result:"ZXCV)" instead of "Tom (ZXCV)".

Does anybody knows the reason behind and can provide a reference if possible?

like image 350
ocean4dream Avatar asked Mar 11 '13 10:03

ocean4dream


People also ask

What is the conditional operator in C++?

The conditional operator is kind of similar to the if-else statement as it does follow the same algorithm as of if-else statement but the conditional operator takes less space and helps to write the if-else statements in the shortest way possible. Syntax: The conditional operator is of the form. variable = Expression1 ? Expression2 : Expression3.

Which operator returns a true value if the condition is false?

This conditional operator will return a true value if the given condition is false. This conditional operator will return a true value if both the given conditions are true and Boolean. This conditional operator will return a true value if either of the given conditions is true and Boolean.

What is null conditional operator (?

Null conditional operator (?.) is another useful addition made to C# 6.0, it allows developers to write cleaner and concise code. We will explore more in detail. We will explore more in detail. In some situations, whenever you invoke a method or property on a object that is NULL .

Why conditional operators are called ternary operators?

Since the Conditional Operator ‘?:’ takes three operands to work, hence they are also called ternary operators. Here, Expression1 is the condition to be evaluated.


2 Answers

You are missing some brackets around your conditional expression.

Try this instead:

string text = "<option value=\"" + cuID.Value + "\">" + cuName + " (" + (cuEmpID == "" ? "-" : cuEmpID) + ")" + "</option>";
htmlResp.Append(text);

As to why the missing brackets caused that to happen... That's an interesting question!

To answer it, let me simplify the original code a bit:

string text = ">>>" + cuEmpID == "" ? "-" : cuEmpID + "<<<"; // Gives "ZXCV<<<"

What's happening is that the conditional expression is using ">>>" + cuEmpID == "" as the condition. This is not equal to "" so the right-hand side of the conditional expression is used, namely the cuEmpID + "<<<" part, which gives the output we're seeing.

You should really simplify the expression, for example:

string normalisedEmpID = cuEmpID == "" ? "-" : cuEmpID;

string text = string.Format
(
    "<option value=\"{0}\">{1} ({2})</option>",
    cuID.Value,
    cuName,
    normalisedEmpID
);
like image 139
Matthew Watson Avatar answered Oct 14 '22 20:10

Matthew Watson


A good example of "don't make expressions too complex".

 "<option value=\"" + cuID.Value + "\">" + cuName 
        + " ("+cuEmpID==""? "-":cuEmpID+")"+  "</option>");

is compiled as

 ("<option value=\"" + cuID.Value + "\">" + cuName+" ("+cuEmpID ) == "" 
      ? "-"
      : cuEmpID+")"+  "</option>");

A simple 'fix' would be

 "<option value=\"" + cuID.Value + "\">" + cuName + " (" 
       + ((cuEmpID == "") ? "-" : cuEmpID ) 
       + ")" + "</option>"

but for readability, use some intermediate variables. ?: should never be nested inside other expressions.

like image 24
Henk Holterman Avatar answered Oct 14 '22 18:10

Henk Holterman