c# conditional operator error

  • Thread starter Thread starter Paul
  • Start date Start date
P

Paul

Can you help me with this error?

string fields = string.Empty;
....
fields.Length > 0 ? fields += ", Name" : fields += "Name";

Error:
Only assignment, call, increment, decrement, and new object expressions can be used as a statement
 
Your code should look like this:

fields += fields.Length > 0 ? ", Name" : "Name";


Brad
 
It's only used for assignment, not for conditional, its behave like the
"return of "value" in case of",
So add the conditional statement at the left site of the phrase as brad
present.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top