C# 如何使用条件函数实现快速内联方法?
这就是我想做的:C# 如何使用条件函数实现快速内联方法?,c#,methods,conditional,inline-method,C#,Methods,Conditional,Inline Method,这就是我想做的: string x = if(true) "String Val" else "No String Val"; 可能吗 string x = condition ? trueValue : falseValue; 您所说的是条件语句: string x = boolVal ? "String Val" : "No String Val"; 如果bool为false时确实希望字符串没有值,则可以更改为: string x = boolVal ? "String Val" :
string x = if(true) "String Val" else "No String Val";
可能吗
string x = condition ? trueValue : falseValue;
您所说的是条件语句:
string x = boolVal ? "String Val" : "No String Val";
如果bool为false时确实希望字符串没有值,则可以更改为:
string x = boolVal ? "String Val" : null;
可能不是这个问题的有效答案(因此需要一个注释):如果您的条件是空检查,例如
string x=(s!=null)?s:“其他东西”
,您可以执行字符串x=s??“其他东西”
只是澄清一下:这些值实际上可以是任何表达式。但它们必须都是字符串,或者在一般情况下,它们必须是相同类型的字符串。