C#中的ref和out关键字有什么区别?

C#中的ref和out关键字有什么区别?

问题描述:

大家好!任何人都可以告诉我ref和out关键字的基本区别吗?

hi everyone!Can any one tell me the basic difference the ref and out keywords?

必须分配一个值:

参考 - 在调用方法之前

out - 在方法内部
must assign a value:
ref - before call the method
out - inside the method


out关键字导致参数通过引用传递。这类似于ref关键字,除了ref要求在传递之前初始化变量。例如:





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. For example:


class OutExample
{
    static void Method(out int i)
    {
        i = 44;
    }
    static void Main()
    {
        int value;
        Method(out value);
        // value is now 44
    }
}







class RefExample
    {
        static void Method(ref int i)
        {
            // Rest the mouse pointer over i to verify that it is an int.
            // The following statement would cause a compiler error if i
            // were boxed as an object.
            i = i + 44;
        }
        static void Main()
        {
            int val = 1;
            Method(ref val);
            Console.WriteLine(val);
            // Output: 45
        }
    }







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.

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:

class RefOutOverloadExample
{
    public void SampleMethod(int i) {  }
    public void SampleMethod(out int i) {  }
}


关键字 out 用于从函数中获取值。



1.你不需要在调用函数中初始化值。

2.需要初始化被调用的值函数,否则编译器将报告错误。



The keyword out is used to get the value from the function.

1. You don't need to initialize the value in the calling function.
2. Need to initialize the value in the called function, otherwise the compiler will report an error.

void funcA(out int a)
{
  a = 0; // at-least one statement should be there that
         // defines the value for the out parameter
         // otherwise compiler will report an error
  ...
}

void funcB() // calling function
{
  ...
  int a;
  funcA(out a); // note: value of a is not initialized.
  ...
}







关键字 ref 是从函数传递并获取修改后的值。



1.您需要先修改变量你调用函数。

2.函数可能会也可能不会改变ref参数的值(如果它没有改变任何东西,那么目的是什么?)





The keyword ref is to pass and get modified value from a function.

1. You need to initialize the variable before you call the function.
2. Function may or may not change the value of the ref parameter (if it doesn't change any thing then whats the purpose?)

void funcA(ref int a)
{
  ...
  a = a + 1; //modifying the value
  ...
}

void funcB() // calling function
{
  ...
  int a = 1;
  funcA(ref a); // note: value of a is not initialized.
  // now a = 2
  ...
}





覆盖只有差异关键字的方法,如下所示,将导致编译时错误无法定义仅在ref和out上有所不同的重载方法





Overriding of methods with only difference keyword, as shown below, will lead to compile time error "cannot define overloaded methods that differ only on ref and out"

static public void funcA(out int a)
{
  a = 1;
}

static public void funcA(ref int a)
{
  a = a + 1;
}







我们可以为out参数函数赋值在调用者函数中。但是,这将导致编译时异常使用未分配的参数a






We can assign a value to the out parameter function in the caller function. However, this will lead to compile time exception "Use of unassigned out parameter a"

namespace ConsoleApplication1
{
    class Program
    {
        static public void funcA(out int a)
        {
            if (a == 1)
                a = 2;
            else a = 1;
        }
        static void Main(string[] args)
        {
            int a = 1;
            funcA(out a);
        }
    }
}





HTH



HTH