C#中一個不常用的特殊運算符——"??"

using System;
using System.Collections.Generic;
using System.Text;
namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            string a = "test1";
            string b = null;
            string c = a ?? "test2";
            string d = b ?? "test3";
            Console.WriteLine("a:{0} b:{1} c:{2}, d:{3}", a, b, c, d);
            Console.ReadLine();
        }
    }
}



程序應該輸出什麼呢?

這個"??"運算符我是在CommunityServer裏面看到的,在裏面大量的使用這個運算符 ,當時還看的莫名其妙 ,好像"? : "這個三元運算符.
不過它完成的功能確實和"? : "相似 ,只是在特定條件下更簡潔了.
輸出的內容是這個: 
a:test1 b: c:test1, d:test3
補充一下: 

??左邊的內容如果不爲空則返回左值的內容,如果爲空則返回右值的內容。 
不過這個符號貌似不常用,或者基本沒看到什麼人用,畢竟不廣泛
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章