Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/297.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何使用条件函数实现快速内联方法?_C#_Methods_Conditional_Inline Method - Fatal编程技术网

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??“其他东西”
只是澄清一下:这些值实际上可以是任何表达式。但它们必须都是字符串,或者在一般情况下,它们必须是相同类型的字符串。