现有新操作符??与?.  余书之

using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace ConsoleApp2
{
    class Program
    {
        static void Main(string[] args)
        {
            myFun(null); // 123
            myFun("456");//  456

            Person personA = new Person() { name = "chenyishi" };
            Console.WriteLine(personA?.name);

            personA = null;
            Console.WriteLine(personA?.name);//person==null,仍不会报错
        }


        static void myFun(string argA)
        {
            Console.WriteLine(argA ?? "123"); //argA==null,则输出123
        }
    }

    class Person
    {
        public string name { get; set; }
    }
}

 

posted on 2018-01-22 16:38  chester·chen  阅读(127)  评论(0编辑  收藏  举报