C#實現函式預設值和C#4.0實現預設值

zhiqiang21發表於2012-05-14
static void Main(string[] args)
        {
            SayHello("侯志強", 80);
            SayHello("侯志強");

            Console.ReadKey();
        }

        C#4.0實現引數的預設值
        static void SayHello(string strName,int nAge=20)
        {
            Console.WriteLine("我的名字是{0},今年{1}歲", strName, nAge);
        }

        //C#4.0以前實現引數預設值
        static void SayHello(string strName1, int nAge)
        {
            Console.WriteLine("我的名字是{0},今年{1}歲", strName1, nAge);
        }

        static void SayHello(string strName1)
        {
            SayHello(strName1, 20);
        }

相關文章