using System;
class Method {
    public static void OutParam(out int o)
    {
     o=100;
   }
   public static int Main() {
     int x;
     OutParam(out x);
     Console.WriteLine("这是 OutParam之后的x={0}",x);
     return 0;
   }
}
调用OutParam(out x);后那o的值就会传给x,这个是怎 么实现的?不是太理解哦。

解决方案 »

  1.   

    Read MSDN
    The out keyword causes arguments to be passed by reference. This is similar to the ref keyword, except that ref requires that the variable be initialized before being passed. To use an out parameter, both the method definition and the calling method must explicitly use the out keyword. For example:  Copy Code 
    class OutExample
    {
        static void Method(out int i)
        {
            i = 44;
        }
        static void Main()
        {
            int value;
            Method(out value);
            // value is now 44
        }
    }
     Although variables passed as an out arguments need not be initialized prior to being passed, the calling method is required to assign a value before the method returns.The ref and out keywords are treated differently at run-time, but they are treated the same at compile time. Therefore methods cannot be overloaded if one method takes a ref argument and the other takes an out argument. These two methods, for example, are identical in terms of compilation, so this code will not compile:  Copy Code 
    class CS0663_Example 
    {
        // compiler error CS0663: "cannot define overloaded 
        // methods that differ only on ref and out"
        public void SampleMethod(out int i) {  }
        public void SampleMethod(ref int i) {  }
    }
     Overloading can be done, however, if one method takes a ref or out argument and the other uses neither, like this:  Copy Code 
    class RefOutOverloadExample
    {
        public void SampleMethod(int i) {  }
        public void SampleMethod(out int i) {  }
    }
     Res
    Properties are not variables and therefore cannot be passed as out parameters.For information on passing arrays, see Passing Arrays Using ref and out.Example
    Declaring an out method is useful when you want a method to return multiple values. A method that uses an out parameter can still a variables as a return type (see return) but it can also return one or more objects to a calling method as out parameters. This example uses out to return three variables with a single method call. Note that the third argument is assigned to null. This allows methods to return values optionally.  Copy Code 
    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;
        }
    }
     
      

  2.   

    public static int Main() {
    int x;
    OutParam(out x);
    return 0;
    }这个时Main,然后调OutParam() 传入 x,OutParam(out int o)里给o赋了个值o=100,也就相当于给x赋值了,然后就没什么可说得了