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?
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.
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.
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 .
Since the Conditional Operator ‘?:’ takes three operands to work, hence they are also called ternary operators. Here, Expression1 is the condition to be evaluated.
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
);
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With