你如何执行if then else语句作为连接?

Chr*_*uce 0 c# if-statement

例如:

StringBuilder list1 = new StringBuilder();
StringBuilder list2 = new StringBuilder();


sb.Append("alert('Operation has been submitted, however, the following files were not valid and thus operations against their respective courses were not processed: "
 + if x == 1 display list1.ToString() else if x==2 display list2.ToString() +
". An email will be sent when the process has completed for any applicable valid course IDs.");
Run Code Online (Sandbox Code Playgroud)

这甚至可能吗?

Rob*_*vey 13

使用三元运算符.

string message = x == 1 ? "message1" : x == 2 ? "message2" : "message3"
Run Code Online (Sandbox Code Playgroud)

  • 一些括号可能有助于澄清,因为您执行两个三元操作,因为OP不熟悉它们. (3认同)