using System; class Program { static void Main(string[] args) { MyClass a1 = null; int a2; MyMethod(out a1, out a2); Console.WriteLine(a1.val); Console.WriteLine(a2); } static void MyMethod(out MyClass f1 , out int f2) { f1 = new MyClass(); f1.val = 25; f2 = 15; } } public class MyClass { public int val = 20; }
1 2 3 4 5 6
static void Main(){ MyMethod(out MyClass a1 , out int a2); Cosole.WriteLine(a2); Cosole.WriteLine(a1.val); a2++; }
class Program { static void Main(string[] args) { int a = 3, b = 4; ref int max = ref Max(ref a, ref b); Console.WriteLine(++max); Console.WriteLine(a); Console.WriteLine(b); } static ref int Max(ref int a, ref int b) { if (a > b) return ref a; else return ref b; } }
命名参数
只要显式指定参数的名字,就可以以任意顺序在方法中列出实参。
1 2 3 4 5 6 7 8 9 10
class Program { static void Main(string[] args) { int val; val = Calc(c: 4, a: 1, b: 8); Console.WriteLine(val); } static int Calc(int a, int b,int c) { return (a+b) * c; } }
class Program { static void Main(string[] args) { int val; val = Calc(1,2); Console.WriteLine(val); } static int Calc(int a, int b , int c = 4) { return (a+b) * c; } }