引用
- static void ShowDouble(ref int val)
- {
- val *= 2;
- Console.Write("val doubled = {0}",val);
- }
- static void Main(string[] args)
- {
- int myNumber = 5;
- Console.WriteLine("myNumber = {0}",myNumber); //5
- ShowDouble(ref myNumber);
- Console.WriteLine("myNumber = {0}",myNumber);} //10
- }
ps:
1、传入的参数必须是变量(不能是字符串常量或者const),且必须初始化;在函数声明和调用都要用ref
2、也可用out代替上面的ref,但是out的变量可以不用初始化
长风破浪会有时,直挂云帆济沧海!
可通过下方链接找到博主
https://www.cnblogs.com/judes/p/10875138.html