这个代码保证 b 不被修改,这就给别人带来的安慰。而你的所谓“前辈”看来喜欢逃避责任,他一定要告诉你 b 被改变了然后让你猜改变成了什么。 [解决办法] ref是传递参数的地址,ref 传递参数前,必须先对变量初始化(赋值) 好处嘛,大概就不用做一件事情还没完,就要返回值给变量
C# code
class RefExample{ static void Method(ref int i) { i = 44; } static void Main() { int val = 0; Method(ref val); // val is now 44 }} [解决办法] ref 关键字使参数按引用传递。其效果是,当控制权传递回调用方法时,在方法中对参数所做的任何更改都将反映在该变量中。若要使用 ref 参数,则方法定义和调用方法都必须显式使用 ref 关键字。 [解决办法]
传递到 ref 参数的参数必须最先初始化。 这与 out 不同,out 的参数在传递之前不需要显式初始化。
class RefRefExample { static void Method(ref string s) { s = "changed "; } static void Main() { string str = "original "; Method(ref str); // str is now "changed " } }
class OutReturnExample { static void Method(out int i, out string s1, out string s2) { i = 44; s1 = "I 've been returned "; s2 = null; } static void Main() { int value; string str1, str2; Method(out value, out str1, out str2); // value is now 44 // str1 is now "I 've been returned " // str2 is (still) null; } }