If tomorrow never comes

The meaning of life is creation,which is independent an boundless.

导航

ref和out异同

Posted on 2009-04-17 20:49  Brucegao  阅读(477)  评论(0编辑  收藏  举报

1、ref和out是.NET里面的两个关键字。

2、ref:

The ref keyword causes arguments to be passed by reference. The effect is that any changes to the parameter in the method will be reflected in that variable when control passes back to the calling method. To use a ref parameter, both the method definition and the calling method must explicitly use the ref keyword. For example:

class RefExample
{
    
static void Method(ref int i)
    {
        i 
= 44;
    }
    
static void Main()
    {
        
int val = 0;
        Method(
ref val);
        
// val is now 44
    }
}

 

An argument passed to a ref parameter must first be initialized. This differs from out, whose argument do not have to be explicitly initialized before they are passed. For more information, see out.

Although ref and out are treated differently at run time, 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 with regard to compilation, so that 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(ref int i) {  }
    
public void SampleMethod(out int i) {  }
}

 

Overloading can be done, however, if one method takes a ref or out argument and the other uses neither as in the following example:

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

Attention:
Properties are not variables and therefore cannot be passed as ref parameters.

Example:

Passing value types by reference, as demonstrated earlier in this topic, is useful, but ref is also useful for passing reference types. This allows called methods to modify the object to which the reference refers because the reference itself is being passed by reference. The following sample shows that when a reference type is passed as a ref parameter, the object itself can be changed.

class RefRefExample
{
    
static void Method(ref string s)
    {
        s 
= "changed";
    }
    
static void Main()
    {
        
string str = "original";
        Method(
ref str);
        
// str is now "changed"
    }
}

3、out:

The out keyword causes arguments to be passed by reference. This is like the ref keyword, except that ref requires that the variable be initialized before it is passed. To use an out parameter, both the method definition and the calling method must explicitly use the out keyword. For example:

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 out arguments do not have to be initialized before 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 with regard to compilation, so that 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) {  }
}

Attention:

 Properties are not variables and therefore cannot be passed as out parameters.

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 access 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 enables methods to return values optionally.

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;
    }
}

 

 4、总结,看到这里,我想大家都明白了,上面的其实就是MSDN对ref和out诠释。还有不明白的吗,我想没有了。。