out and ref in c

Keywords: C#

I don't know if you've ever encountered a situation where you need a function to return multiple values.

When writing code to return multiple values, of course, you can return an array to implement, but what if there are different types of multiple values to return? What to do at this time?

In c, the out keyword and the ref keyword work. (and out is more suitable for places where multiple return values are used)

 

In a function, if out or ref is used as the keyword before the formal parameter, if the parameter is modified in the function body, the value of the argument assigned to the corresponding parameter will be modified before calling this function. How is this achieved? Its mechanism is that the parameters decorated with out and ref are reference type, not value type, that is to say, the information they store is address, not value type.

 

Since they are all stored addresses, what's the difference between them?
I have sorted out the following differences:
1. ref requires a variable to be initialized (initial value) before it is passed, but when it is called, it can do nothing about it; while out does not need to give an initial value to the variable of this parameter before * is called.
2. Out can only be used to transfer values out of methods. No matter what the external value is, out is used to clear the parameter, and then it must be assigned in the function body. That is, when * is called, the function body of * must assign a value to the variable decorated with out.

In short, ref has in and out (or no out), out has no in and out.

The sample code is as follows,

class Program
    {
      
            static void Main(string[] args)
            {
               Program pg = new Program();
                int x =0;
                int y = 0;
                pg.getnull(ref x, ref y);//Here x,y The initial value must have been assigned to, otherwise the compilation fails
                Console.WriteLine("x=" + x + ",y=" + y);//Output is: x=0,y=0,The original value does not change because it is an empty method

                pg.GetValue(ref x, ref y);//Here x,y Already assigned initial value
                Console.WriteLine("x=" + x + ",y=" + y);//Output is: x=1,y=2
                Console.ReadLine();

            }
            public void getnull(ref int x, ref int y)
            {

            }
            public void GetValue(ref int x, ref int y)
            {
                x++;
                y = y + 2;
            }
       
    }
 class Program
    {
      
            static void Main(string[] args)
            {
               Program pg = new Program();
                int x =0;
                int y = 0;
                pg.getnull(out x,out y);//Here x,y There is no need to assign the initial value. It doesn't matter if it is assigned, but it will be cleared. Note:** Keyword must be written when calling function out ** Because parameters are passed with keywords
                Console.WriteLine("x=" + x + ",y=" + y);//Output is: x=3,y=3,The original value does not change because it is an empty method

                pg.GetValue(out x, out y);//Here x,y Already assigned initial value
                Console.WriteLine("x=" + x + ",y=" + y);//Output is: x=12,y=21
                Console.ReadLine();

            }

            public void getnull(out int x, out int y)//If there is no x,y If the initial value is assigned (i.e. the following function body is empty), the editor will prompt "before the control leaves the current method, the out parameter x Initial value ", compilation failed
            {
                x = 3;//In this case, we assign the value and compile it without assigning it
                y = 3;
            }
            public void GetValue(out int x, out int y)
            {
                //x++;//Again, if you write like this, you will be prompted "unassigned out parameter x",So it can't be so direct x++
                //y = y + 2;//Ditto

                //If we change the writing method below, we can compile
                x = 12;
                y = 12;
            //This function and getnull It's exactly the same
             }
    }

Posted by hob_goblin on Wed, 01 Apr 2020 23:49:05 -0700