处理C#中的空字符串?操作者

And*_*oar 3 c#

我使用以下代码:

string x = str1 ?? str2 ?? str3 ??  "No string";
Run Code Online (Sandbox Code Playgroud)

但是,如果这些字符串(str1,str2,str3)中的任何一个是String.Empty,那么!= null?

我该如何处理这种情况?

por*_*ges 7

你不能用??.你可以使用Linq做这样的事情:

string x = new[]{ str1, str2, str3, "No string" }.First(x => !string.IsNullOrEmpty(x));
Run Code Online (Sandbox Code Playgroud)