c#里边如果在main函数下定义一个简单类型int a;
那么我如何通过一个函数传入这个变量的引用?(传地址)

解决方案 »

  1.   

    方法参数上的 ref 方法参数关键字使方法引用传递到方法的同一个变量。当控制传递回调用方法时,在方法中对参数所做的任何更改都将反映在该变量中。若要使用 ref 参数,必须将参数作为 ref 参数显式传递到方法。ref 参数的值被传递到 ref 参数。using System;
    public class MyClass 
    {
       public static void TestRef(ref char i) 
       {
          // The value of i will be changed in the calling method
          i = 'b';
       }   public static void TestNoRef(char i) 
       {
          // The value of i will be unchanged in the calling method
          i = 'c';
       }   // This method passes a variable as a ref parameter; the value of the 
       // variable is changed after control passes back to this method.
       // The same variable is passed as a value parameter; the value of the
       // variable is unchanged after control is passed back to this method.
       public static void Main() 
       {
       
          char i = 'a';    // variable must be initialized
          TestRef(ref i);  // the arg must be passed as ref
          Console.WriteLine(i);
          TestNoRef(i);
          Console.WriteLine(i);
       }
    }
    输出
    b
    b