using System;
namespace ref参数
{
class Program
{
static void Main(string[] args)
{
/*
ref参数
能够将一个变量带入一个方法中进行改变,改变完成后,再将改变后的值带出方法
要求在方法外必须为其赋值,而方法内可以不赋值
*/
#region 使用方法交换两个int类型变量
int n1 = 10;
int n2 = 20;
Test(ref n1, ref n2);
Console.WriteLine("n1:"+n1);
Console.WriteLine("n2:" + n2);
Console.ReadKey();
}
public static void Test(ref int n1,ref int n2)
{
int temp = n1;
n1 = n2;
n2 = temp;
}
#endregion
}
}
C# ref参数
最新推荐文章于 2024-05-23 13:35:39 发布