Difference between out and ref in C #

Source: Internet
Author: User

Reprinted from http://blog.pfan.cn/goalbell/23233.html

Next I will summarize my views on the out and ref parameters:
1. Similarities Between out and ref reference parameters: parameters are passed to the function through reference;
2. The difference between out and ref reference parameters is that the parameter is passed through ref reference. This parameter must be initialized and cannot be initialized in the function that calls it. The following example is incorrect:

namespace refConsoleApp{    class MyRefClass    {        static void MyRefMethod(ref int i)        {            int i = 20;        }        static void main(string[] args)        {            int value;  //not initialized the value;            MyRefMethod(ref value);            Console.WriteLine("The value is {0}", value);        }    }}
 

3. Use the out parameter to reference multiple parameters to return multiple values, which allows the method to return the required values at will. the following example:

namespace multi_outConsoleApp{    class MyMultipleoutClass    {        static void MyMultipleMethod(out int i, out string str1, out string str2)        {            i = 20;            str1 = "I was born";            str2 = "zhaoqing";        }        static void Main(string[] args)        {            int value;            string s1, s2;            MyMultipleMethod(out value, out  s1, out  s2);            Console.WriteLine("The integer value is {0}\nThe first string value is {1}\nThe second string value is {2}", value, s1, s2);        }    }}

The result is as follows:

The integer value is 20

The first string value is I was born

The second string value is Zhaoqing


4. if one method uses ref to reference a parameter, and the other method uses out to reference the parameter, the functions with the same method name cannot be overloaded. Otherwise, a compilation error occurs. The following example shows: "cannot define overloaded methods that differ only on ref and out"

namespace overloadofConsoleApp{    class overloadofclass    {        static void MyMethod(ref int i)        {            i = 20;        }        static void MyMethod(out int i)        {            i = 40;        }        static void Main(string[] args)        {            int refvalue = 0;            MyMethod(ref refvalue);            Console.WriteLine("The value is {0}", refvalue);            int outvalue;            MyMethod(out outvalue);            Console.WriteLine("The value is {0}", outvalue);        }    }} 

The following example is correct:

namespace overloadofConsoleApp{    class overloadofclass    {        static void MyMethod(ref int i)        {            i = 20;        }        static void MyMethod(int i)        {            i = 40;        }        static void Main(string[] args)        {            int refvalue = 0;            MyMethod(ref refvalue);            Console.WriteLine("The value is {0}", refvalue);            int outvalue = 0;            MyMethod(outvalue);            Console.WriteLine("The value is {0}", outvalue);        }    }}  
The result is as follows:
The value is 20
The value is 0
5. Use the ref parameter to change the reference type, because the reference type is passed as the ref parameter and the object will be changed. The following example:
namespace refConsoleApp{    class refclass    {        static void RefMethod(ref string s)        { s = "I was born in zhaoqing"; }        static void Main(string[] args)        {            string str = "I am goalbell"; RefMethod(ref str);            Console.WriteLine("The value is {0}", str);        }    }}

Display result:
The value is I was born in Zhaoqing

6. you can use ref and out to pass the array. When using ref to pass the array, you must Initialize an array in the main function, in the called method, you can specify an array as a null value or initialize it into a different array. When an array is passed out, it is determined to assign a value to an array in the called method. the following example distinguishes them:

namespace refszConsoleApp{    class refszclass    {        static void RefszMethod(ref int[] arr)        {            if (arr == null)  //assigned the null value            {                arr = new int[5];  //initialized to a different array            }        }        static void Main(string[] args)        {            int[] theArray ={ 1, 2, 3, 4, 5 };//initialize the theArray            RefszMethod(ref theArray); // pass the value using of ref            Console.WriteLine("Array elements are:");            for (int i = 0; i < theArray.Length; i++)            {                Console.WriteLine(theArray[i] + "");            }        }    }}namespace outszConsoleApp{    class outszclass    {        static void OutszMethod(out int[] arr)        {            arr = new int[5] { 1, 2, 3, 4, 5 };//initialize the arr        }        static void Main(string[] args)        {            int[] theArray;            OutszMethod(out theArray);            Console.WriteLine("Array elements are:");            for (int i = 0; i < theArray.Length; i++)            {                Console.WriteLine(theArray[i] + "");            }        }    }}

The preceding example is compiled in Visual C #2005.

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.