1、ref参数方法
ref用于修饰方法的参数,表示该参数为引用传递,而不是值传递,例如:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Fuction
{
class Program
{
static void Main(string[] args)
{
int testParameter;
testParameter = 10;
RefTest(ref testParameter);
Console.WriteLine(testParameter);
Console.ReadKey();
}
private static void RefTest(ref int parameter)
{
parameter = parameter + 1;
}
}
}
需要注意的地方有一下几点:
- ref参数类型的方法在定义与调用的时候都需要加ref
- ref参数在调用前必须初始化,这个与out参数不同,后者不需要初始化,这也是两者的区别
- 希望在调用函数时候修改被传入的参数时候可以使用ref类型的参数
2、out参数方法
out参数与ref