第一节 49_ref_out

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace _49_ref_out
{
    class Program
    {
        static void Main(string[] args)
        {
            /* 函数的ref out参数
             * 函数参数默认的值传递的,也就是"复制一份",例子:
             * int age = 20;
             * Int Age(age);
             * Console.WriteLine("age={0}",age);
             * ref必须先初始化,因为是引用,所以必须先"有",才能引用,而out则是内部为外部赋值,所以不需要初始化,而且初始化也没用
             * ref应用的景内部对外部的值进行改变,out则是内部对外部变量的赋值,out一般用在函数有多个返回值的场所
             * 两个变量的交换 int.TryParse
            */
            int i = 5;
            IncAge(ref i);
            Console.WriteLine("年龄为;{0}",i);

            int age=100;
            IncAgeOut(out age);
            IncAgeOut(out age);
            Console.WriteLine("年龄为;{0}", age);



            Console.ReadKey();

        }

        static void IncAgeOut(out int age) 
        {
            age = 55;
        }

        //引用
        static void IncAge(ref int age) 
        {
            age += 5;
        }
    }
}

  

posted @ 2012-02-21 23:32  简单--生活  阅读(209)  评论(0编辑  收藏  举报
简单--生活(CSDN)