"ASP. 1200 Example" ref keyword and out keyword

Source: Internet
Author: User

out keyword causes arguments to be passed by reference. ' >ref keyword

outKeyword causes arguments to being passed by reference. ' >ref keyword causes an argument to is passed by reference, not by value. '  The >ref keyword causes a parameter to be passed by reference, not a value. the effect of passing by reference is that any changes to the parameters in the method are reflected in the caller's underlying parameters. The value of the reference parameter is always the same as the value of the underlying parameter variable.

do not confuse the "pass by reference" concept with the "reference type" concept.   These two concepts are different.   ref regardless of whether it is A value Type or a reference type. ' The > method parameter can be decorated by  , whether it is a value type or a reference type, ref  .   No value types are boxed when passing value types by reference.

to use ref parameters, method definitions, and methods that are called must explicitly use the keyword, ref . For example:

classRefexample {Static voidMethod (ref inti) {//Rest The mouse pointer over I-Verify that it's an int. //The following statement would cause a compiler error if I//were boxed as an object.i = i + -; }        Static voidMain () {intval =1; Method (refval);            Console.WriteLine (Val); //output:45        }    }

out keyword causes arguments to be passed by reference. ' > Outkeyword

out keyword causes arguments to being passed by reference. ' out   keyword causes parameters to be passed by reference.   ref keyword, except this ref requires that The variable was initialized before it is passed. ' > This is similar to the  ref  keyword, except that the   ref   requires that the variable be initialized before it is passed.  

out parameter, both the method definition and the calling method must explicitly use the out keyword. ' > To use an out parameter, both the method definition and the calling method must explicitly use the Out keyword. For example:

classoutexample{Static voidMethod ( out inti) {i= -; }    Static voidMain () {intvalue; Method ( outvalue); //value is now    }}

out arguments does not has to be initialized before being passed, the called method was required to assign a value Before the method returns. ' > Although a variable passed as an out parameter does not have to be initialized before it is passed, the called method needs to assign a value before returning.

out arguments does not has to be initialized before being passed, the called method was required to assign a value Before the method returns. ' Simultaneous use of >out and ref

refand outKeywords cause different run-time behavior, they is not considered part of the the method signature at compile time. ' > AlthoughrefAnd outKeywords cause different run-time behavior, but they are not considered part of the method signature at compile time. refArgument and the other takes an outArgument. ' > Therefore, if the only difference between the two methods is: one acceptsrefparameter, another accepts outparameter, you cannot overload both methods. For example, the following code is not compiled:

class cs0663_example{    //  Compiler error CS0663: "Cannot define overloaded     // Methods that differ only on ref and out ".     Public void SampleMethod (outint  i) {}    publicvoid SampleMethod ( ref int i) {}}

ref or out argument and the other uses neither, like this: ' > However, if a method takes a ref or out parameter , and the other method does not take these two types of parameters, it can be overloaded as follows:

class outoverloadexample{    publicvoid samplemethod (int  i) {}      Publicvoid samplemethod (outint5;}}

out parameters. ' The > property is not a variable and therefore cannot be passed as an out parameter.

passing Arrays Using ref and Out (C # Programming Guide). ' > For information about passing arrays, see passing arrays with ref and Out (C # Programming Guide).

ref and out keywords for the following kinds of methods: "> cannot be used for the following methods   ref   and   out   Keywords:

    • async modifier. ' > Async method, or use the  async  modifier, defined.

    • yield return or yield break statement. ' > Iterator method, including a   will return   or   yield break   statement.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.