ITeye博客

C#函数参数传递 ref关键字

diaochenlong2 · 2013-01-01 21:39 · C# · 编程语言 · Markdown

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

namespace ConsoleApplication2 { class Program { static void Swap(ref int x,ref int y) { int temp = x; x = y; y = temp; }

    static void Main(string[] args)
    {
        int a = 40;
        int b = 80;
        Console.WriteLine("交换前: a = {0},b = {1}",a,b);
        Swap(ref a,ref b);
        Console.WriteLine("交换后: a = {0},b = {1}",a,b);
        Console.ReadKey();
    }
}

}

交换前: a = 40,b = 80 交换后: a = 80,b = 40 ref修饰的变量必须要赋值

评论(0)

暂无评论。

评论功能维护中,当前仅展示历史评论。