When to use ref and out?
本问题已经有最佳答案,请猛点这里访问。
所以我在为我的大学班做一个非常简单的实验室,遇到了一些问题。我的老师没有详细解释你什么时候用REF,什么时候用掉。分配是将预先编写的方法更改为使用ref,然后确保它已运行,然后将其更改为使用out。我把参考部分记下来了,但是我该怎么重写这个方法来使用呢?下面是程序
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 | using System; static class Program { /// <summary> /// Purpose: Entry point to your C# program /// </summary> static void Main() { int iVal1 = 5; int iVal2 = 7; //Call the Swap method with two arguments Swap(ref iVal1, ref iVal2); Console.WriteLine("Swapped values first {0:D} second {1:D}", iVal1, iVal2); Console.WriteLine("Press Enter to continue ..."); Console.ReadLine(); }//End Main() /// <summary> /// Purpose: To swap the two parameters passed to this method /// </summary> /// <param name="num1">num1 int, first number</param> /// <param name="num2">num2 int, second number</param> static public void Swap(ref int num1, ref int num2) { int tempInt = num1; num1 = num2; num2 = tempInt; } }//End class Program |
当您想初始化属性的引用时,