区别ref参数传递与值传递的区别

来源:互联网 发布:拍淘宝女装用什么镜头 编辑:程序博客网 时间:2024/05/21 07:08

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

namespace ConsoleApplication3
{
    class MainClass
    {
        public static void Swap(ref int a, ref int b)
        {
            int temp = a;
            a = b;
            b = temp;

        }
        public static void Swap(int a, int b)
        {
            int temp = a;
            a = b;
            b = temp;
        }
        public static void Main(string[] args)
        {
            Console.WriteLine("Ref Test");
            int x = 1;
            int y = 2;
            Swap(x, y);
            Console.WriteLine("After Swap, x:{0},y:{1}", x, y);
            int u = 1;
            int v = 2;
            Swap(ref u, ref v);
            Console.WriteLine("After Swap, u:{0},v:{1}", u, v);
            Console.ReadLine();

        }


    }
}

原创粉丝点击