结合多次合并?

cae*_*say 0 c# null-coalescing-operator

C#中有什么东西可以让你做一些像

string str = nullval1 ?? nullval2 ?? nullval3 ?? "Hi";
Run Code Online (Sandbox Code Playgroud)

它会从左到右选择第一个非空的?

如果这个操作符不这样做,是否有可能用最少的代码提供类似的功能?

Jon*_*eet 5

这样做绝对没问题.示例代码:

using System;

class Program
{
    static void Main(string[] args)
    {
        string x = null;
        string y = "y";
        string z = "z";

        Console.WriteLine(x ?? y ?? z); // Prints "y"
    }
}
Run Code Online (Sandbox Code Playgroud)

  • @Tommy:总有你友好的邻居编译器!:d (2认同)