C # ref and out summary

Source: Internet
Author: User

The transfer of parameters is generally divided into two types: one is "value passing" i.e. a copy of the passed argument, and since it is a copy, any action on the parameter in the function will not be reflected in the original argument. The other is "reference passing", which is: the address where the argument is passed (the parameter and the argument point to the same memory address), then any changes to the parameter in the function are reflected in the original argument.

The two ways to implement reference passing in C # are: Ref and out. Of course, these two ways also have their differences, the following will gradually explain their differences.

  1. When using the REF keyword for "reference passing", the passed-in argument must first be initialized, just like a pointer in C and C + +, it must be assigned a value (to point it to a specified memory location), otherwise it does not necessarily point to the memory where, so it is dangerous, not allowed. When you use the Out keyword for reference passing, the incoming arguments do not have to be initialized first. As shown in the following example:1using System;
    2using System.Collections.Generic;
    3using System.Linq;
    4using System.Text;
    5
    6namespace Refandout
    7{
    8 Class Refandouttesting
    9 {
    Ten static void Main (string[] args)
    11 {
    int x;
    int y;
    Outtest (out x, out y);
    Console.WriteLine (String. Format ("x = {0}, y = {1}", X, y));
    16
    + int a = 100;
    int b = 200;
    Reftest (ref a, ref B);
    Console.WriteLine (String. Format ("a = {0}, y = {1}", A, b));
    21}
    The//out parameter does not have to be initialized before it is used
    public static void Outtest (out int first, out int second)
    25 {
    First = 1;
    Second = 2;
    29}
    The//ref parameter must be initialized before use
    public static void Reftest (ref int first, ref int second)
    32 {
    first = 1111;
    second = 2222;
    35}
    36}
    37}

  2. Although using the Out keyword for "reference passing", the passed-in arguments do not have to be initialized first, but in the function you must first initialize the parameters before you use them. Because: When the out parameter is used, the program first empties the Out argument (so that the actual argument is not used for this function first) and then the parameter is manipulated accordingly; the initialization of the parameter must be completed before leaving the function (even if you do not do anything to the parameter). Otherwise, the pointer to the parameter does not know where to point. If you change the Outtest function above: 1 public static void Outtest (out int first, out int second)
    2 {
    3 first = second;
    4//first = 1;
    5//second = 2;
    6} will prompt you: use of unassigned out Parmeter ' second '. The same error occurs even if you first initialize the argument (because the parameter is emptied first)1 using System;
    2 using System.Collections.Generic;
    3 using System.Linq;
    4 using System.Text;
    5
    6 namespace Outandref
    7 {
    8 class Program
    9 {
    Ten static void Main (string[] args)
    11 {
    The//out variable does not have to be displayed before it is used
    int x = 1111;
    int y = 2222;
    Outtest (out x, out y);
    Console.WriteLine ("x = {0}, y = {1}", X, y);
    17}
    18
    public static void Outtest (out int first, out int second)
    21 {
    22//Before you leave this function, you must assign a value to first and second, otherwise you will get an error.
    At first = second;
    24//above this line error, because after the use of the out, first and second are emptied, need to be re-assigned, even if the function before the call to assign a value is not
    First = 1;
    Second = 2;
    27}
    29}
    31}
    32

  3. So the above two points can be summed up as: Ref has a go in, out have infested.
  4. Use ref and out for reference passing, and when defining methods and calling methods, add the ref and out keyword before the parameter to satisfy the match.
  5. Because the property is a method, not a variable, and the pointer is a variable. Therefore, none of the attributes can be passed as ref and out parameters. If you declare two properties in the Refandouttesting class above and pass it to the Reftest and Outtest methods, an error will occur!1 using System;
    2 using System.Collections.Generic;
    3 using System.Linq;
    4 using System.Text;
    5
    6 namespace Refandout
    7 {
    8 Class Refandouttesting
    9 {
    10//attribute (is method, not variable) so it cannot be used as ref and out parameters
    public static int Firstnumber
    12 {
    get;
    Set;
    15}
    public static int Secondnumber
    17 {
    get;
    Set;
    20}
    21st
    22//fields can be used as ref and out parameters
    The public static int mynumber;
    public static int yournumber;
    25
    -static void Main (string[] args)
    27 {
    28//Correct
    Outtest (out MyNumber, out yournumber);
    Console.WriteLine ("MyNumber = {0}, Yournumber = {1}", MyNumber, Yournumber);
    Reftest (ref mynumber, ref yournumber);
    Console.WriteLine ("MyNumber = {0}, Yournumber = {1}", MyNumber, Yournumber);
    33
    34//Error
    Outtest (out Firstnumber, out secondnumber);
    Console.WriteLine ("Firstnumber = {0}, Secondnumber = {1}", Firstnumber, Secondnumber);
    PNS Reftest (ref firstnumber, ref secondnumber);
    Console.WriteLine ("Firstnumber = {0}, Secondnumber = {1}", Firstnumber, Secondnumber);
    45}
    46}
    47}

C # ref and out summary

Related Article

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.