引數的傳遞方式?
參考:稍微靠靠腰
Code:
using System; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace ConsoleApplication2 { class Program { static void Main(string[] args) { Console.WriteLine("\n *** 一般函式傳遞 *** \n"); int a = 10, b = 20; Console.WriteLine("\n 未進入函式前\t\t:a={0} \t b={1}", a, b); CallValues(a, b); Console.WriteLine("\n 進入函式後\t\t:a={0} \t b={1}", a, b); CallValuesUseRef(ref a, ref b); Console.WriteLine("\n 進入函式後\t\t:a={0} \t b={1}", a, b); int initX, initY; Console.WriteLine("\n 進入函式前\t\t:initX,initY 沒有初始值"); CallValuesUseOut(out initX, out initY); Console.WriteLine("\n 進入函式後\t\t:initX={0} \t initY={1}", initX, initY); Console.Read(); } private static void CallValuesUseOut(out int x, out int y) { int z; x = 20; y = 30; Console.WriteLine("\n 函式內 交換前\t\t:x={0} \t y={1}", x, y); z = x; x = y; y = z; Console.WriteLine("\n 函式內 交換後\t\t:x={0} \t y={1}", x, y); } private static void CallValuesUseRef(ref int x, ref int y) { int z; x = 20; y = 30; Console.WriteLine("\n 函式內 交換前\t\t:x={0} \t y={1}", x, y); z = x; x = y; y = z; Console.WriteLine("\n 函式內 交換後\t\t:x={0} \t y={1}", x, y); } private static void CallValues(int x, int y) { int z; x = 20; y = 30; Console.WriteLine("\n函式內 交換前\t\t:x={0} \t y={1}", x, y); z = x; x = y; y = z; Console.WriteLine("\n函式內 交換後\t\t:x={0} \t y={1}", x, y); } } }結果:
沒有留言:
張貼留言