using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;namespace ConsoleApplication1
{class Class1{static void Main(string[] args){// 传值int i1 = 1, i2 = 2;swap(i1, i2);Console.WriteLine("{0} {1}", i1, i2);// 传址,ref传址之前一定要先赋值int i3 = 1, i4 = 2;swap(ref i3, ref i4);Console.WriteLine("{0} {1}", i3, i4);// 传址,out传址之前不用先赋值,即使赋值了也没用,传址以后一定要赋值int i5 = 1, i6 = 2;swap2(out i5, out i6);Console.WriteLine("{0} {1}", i5, i6);Console.ReadKey();}/*** 交换数据i1和i2*/static void swap(int i1, int i2){int temp;temp = i1;i1 = i2;i2 = temp;}/*** 交换数据i1和i2*/static void swap(ref int i1, ref int i2){int temp;temp = i1;i1 = i2;i2 = temp;}/*** 交换数据i1和i2*/static void swap2(out int i1, out int i2){// 传址以后一定要赋值i1 = 5;i2 = 6;int temp;temp = i1;i1 = i2;i2 = temp;}}
}
详细解决方案
传址,ref,out
热度:0 发布时间:2024-01-17 03:36:47.0
相关解决方案