---
title: "C#函数参数传递 ref关键字"
author: "diaochenlong2"
date: "2013-01-01T21:39:22Z"
tags: []
categories: ["编程语言"]
slug: "c-ref"
draft: false
lastmod: "2013-01-01T21:40:02Z"
id: 1758347
url: "https://diaochenlong2.iteye.com/blog/1758347"
---

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修饰的变量必须要赋值