C# ?\?:\??\?. 操做符\運算符

static void Main(string[] args)
        {
            //?表示值類型的可空操做符
            int? a1 = null; 
            DateTime? myDateTime = null;

            //三元(運算符)表達式(?:)
            int b = 0;
            int a2 = b > 0 ? 1 : 2;

            //空合併運算符(??),若是a1爲null值,則a1的值爲1
            int a3 = a1 ?? 1;

            //NULL查看運算符(?.):假如對象爲NULL,則不進行後面的獲取成員的運算,直接回來NULL
            int?[] array1 = null;
            string data = array1?.FirstOrDefault()?.ToString();
            Console.WriteLine(data);
            Console.ReadLine();
        }
相關文章
相關標籤/搜索