On the ref and out parameters in C #

Source: Internet
Author: User
Tags variable scope

Many beginners (even developers who work for a certain amount of time), when they encounter ref or out parameters, are always a bit "dizzy" or confused, and do not know exactly when and where to use ref or out parameters.

This article will give you a detailed explanation of the ref and out parameters in C # through examples and explanations.

Using System;
Using System.Collections.Generic;
Using System.Linq;

Using System.Text;
            Namespace Refandout {class Program {static void Main (string[] args) {int age = 10;
            Incage (age);
            Console.WriteLine ("The value of age in the main function is:" +age);//print out an int score = 80;
            Incscore (ref score);
            Console.WriteLine ("The value of score in main function is:" + score);//print out Bayi int i=99;
            Init (out i);

        Console.WriteLine ("The value of I in the main function is:" + i);//Print out a console.readkey ();
            public static void Incage (int myage) {myage++; 
        Console.WriteLine (the value of myage in the Incage function is: + myage);//Print out one} public static void Incscore (ref int Myscore)
            {myscore++; 
        Console.WriteLine ("The value of Myscore in the Incscore function is:" + myscore);//Print out Bayi} public static void Init (out int II)
            {II = 10; ConsoLe. WriteLine ("The value of II in the INIT function is:" + II);//Print out 10}/* Description: Method pass value in C #, regardless of parameter type (value type or reference type), default is "value delivery".
         Except for ref and out.
         * In the above code, when the Incage method is invoked, the value of the method's parameter myage changes, but it does not affect the value of the age variable in the main function. * Even if I named "Age" the parameters of the Incage function, the value of the age variable in the main function will not change.
         Because it is not the same variable (reference: variable scope).
         * And when the Incscore function is invoked, changes to its parameter Myscore (ref type) directly affect the value of the score variable in the external main function.
         * It can be seen that when a parameter of type ref is used, the "reference" of the parameter is passed, affecting the value of the variable defined outside the function. * and in the final init, the output parameters of the out type are used. Also has an effect on the outside of the function.
         Out type parameter, which is appropriate to assign an initial value to an external variable in a function.
 */
    }
}

After reading the appeal example, the reader may wish to knock the code to verify it. This experience is more profound. When you really understand a technology, you know when to use it.

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.