C# 使用ref参数

ref 标识传入参数是按址传递,如果在调用过程中发生更改,那么在调用方法结束后传入的参数值也会发生改变
    class Program
    {
        static void Main(string[] args)
        {
            string a = "001";
            int b = 0;

            Console.WriteLine("call before");
            Console.WriteLine("a: {0}", a);
            Console.WriteLine("b: {0}", b);

            TestRef(ref a, ref b);

            Console.WriteLine("");
            Console.WriteLine("call after");
            Console.WriteLine("a: {0}", a);
            Console.WriteLine("b: {0}", b);

            Console.Read();
        }

        static void Test(ref string a, ref int b)
        {
            a = "100";
            b = 100;
        }
    }


热门推荐