Parse the ref and out parameters in C

  • 2020-05-24 06:02:45
  • OfStack

Many beginners (and even developers who work a certain amount of time) get a little "dizzy" or confused when they encounter the ref or out parameters, and don't know exactly when or under what circumstances to use the ref or out parameters.

This article will explain the ref and out parameters in C# in detail through examples and instructions.


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("Main In the function age The value is: "+age);// Print out the  10
            int score = 80;
            IncScore(ref score);
            Console.WriteLine("Main In the function score The value is: " + score);// Print out the  81
            int i=99;
            Init(out i);
            Console.WriteLine("Main In the function i The value is: " + i);// Print out the  10

            Console.ReadKey();
        }
        public static void IncAge(int myAge) 
        {
            myAge++;
            Console.WriteLine("IncAge In the function myAge The value of :" + myAge);// Print out the  11
        }
        public static void IncScore(ref int myScore) 
        {
            myScore++;
            Console.WriteLine("IncScore In the function Myscore The value of :" + myScore);// Print out the  81
        }
        public static void Init(out int ii ) 
        {
            ii = 10;
            Console.WriteLine("Init In the function ii The value of :" + ii);// Print out the  10
        }
        /*
         *  Description: C# The default is "value pass", regardless of the parameter type (value type or reference type). ref and out With the exception of. 
         *  In the code above, called IncAge After the method, the parameters of the method myAge Is changed, but it doesn't matter Main In the function age The value of the variable. 
         *  Even if I put the IncAge The parameter of the function is also named" age ", Main In the function age The value of the variable doesn't change. Because it's not the same at all 1 Variables (see variable scope). 
         *  And when called IncScore Function after its parameters myScore ( ref When a change occurs, it directly affects the outside Main In the function score The value of the variable. 
         *  This can be seen: when used ref When a parameter of type is passed, a "reference" to the parameter is passed, which affects the value of the variable defined outside the function. 
         *  And then at the end Init In, used Out Output parameter of type. It also affects the outside of the function. out Type parameter, suitable for assigning initial values to external variables in a function. 
         */
    }
}

After looking at the appeal example, the reader might as well hit the code to verify 1. It's more profound. When you really understand a technology, you know when to use it.


Related articles: