我正在尝试创建 MailMessage,但出现以下错误...
Cannot implicitly convert type 'string' to 'bool'
这是我的初始化语句:
MailMessage msg = new MailMessage("DoNotReply@optoma.com",
myTbl.Rows[i]["Requester"].ToString().Trim(),
subject,
"Dear " + myTbl.Rows[i]["Ship_Attention"].ToString() + ",<br/><br/>" +
body + "<br/>Your ISO ID is " + myTbl.Rows[i]["ISO_ID"].ToString() +
(Convert.ToInt32(myTbl.Rows[i]["EmailType"]) == 1) ?
("<br/>Tracking Number: " + myTbl.Rows[i]["Tracking_No"].ToString()) :
("") + "<br/><br/>Please examine the loaned items for this transaction:<br/><br/>" +
sw.ToString());
我试图在运行时基于 bool 表达式添加到字符串。为什么我不能这样做?我做的不对吗?
string + (true | false) ? "somestring" : "anotherstring" + string
最佳答案
那个? : 运算符的优先级非常低。将它放在括号中,我认为您会解决您的问题。
((true|false)?"somestring":"anotherstring")
关于c# - 三元运算符;这个语法有什么问题?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5268107/