C# ref/out usage

Table of contents

1. Introduction

2. The ref keyword

the case

Note 1

Note 2

Three, out keyword

the case

Note 1

Note 2

4. Similarities between ref and out keywords

Five, the difference between ref and out keywords

Finish


1. Introduction

In C#, the ref and out keywords are used in the way of parameter passing. They allow parameters to be modified inside a method, and the modified value is carried back to the calling method. The ref keyword is used to pass parameters of reference type. When using the ref keyword to pass parameters, any modification of the parameters inside the method will affect the original variables in the calling method. The out keyword is mainly used to pass parameters of value type, and requires assignment of parameters inside the method. Unlike ref, when using the out keyword to pass parameters, there is no need to initialize the parameters before calling the method.

2. The ref keyword

the case

namespace RefAndOut
{
    internal class Program
    {
        static void Main(string[] args)
        {
            int number = 5;
            UpdateValue(ref number);
            Console.WriteLine(number);

            Console.ReadKey();
        }

        static void UpdateValue(ref int value)
        {
            value = 10;
        }
    }
}

run:

Note 1

The variable passed by ref must be initialized, otherwise an error will be reported

 

 

Note 2

ref parameters are not assigned within the method and will have no effect

namespace RefAndOut
{
    internal class Program
    {
        static void Main(string[] args)
        {
            int number = 5;
            string name = "test";
            UpdateValue(ref number, ref name);
            Console.WriteLine(number);
            Console.WriteLine(name);

            Console.ReadKey();
        }

        static void UpdateValue(ref int value, ref string name)
        {
            //value = 10;
            //name = "tt";
        }
    }
}

run:

 

Three, out keyword

the case

namespace RefAndOut
{
    internal class Program
    {
        static void Main(string[] args)
        {
            int age;
            string name;
            Test(out age, out name);
            Console.WriteLine("age:{0}, name:{1}", age, name);

            Console.ReadKey();
        }

        static void Test(out int age, out string name)
        {
            age = 16;
            name = "张三";
        }
    }
}

run:

 

Note 1

The out keyword must be assigned inside the method body, otherwise an error will be reported

 

Note 2

Using the out parameter, the executed variable may not be initialized

 

 

4. Similarities between ref and out keywords

1. Both are passed through function parameters:

Both ref and out are used to pass parameters by reference, and the value of the passed parameter can be modified inside the method.

2. Both can be initialized before calling the method

Both ref and out can be initialized before calling the method. After calling the method, the actual value of the parameter is subject to the assignment inside the method.

Five, the difference between ref and out keywords

1. Initialization of parameters:

When using the ref keyword to pass parameters, the parameters must be initialized (assigned) before calling the function, and when using the out keyword to pass parameters, the parameters can be initialized inside the called function.

2. Assignment inside the method:

When using the ref keyword to pass parameters, no assignment can be made inside the function, and when using the out keyword to pass parameters, the function must be assigned a value

Finish

If this post is helpful to you, please follow + like + leave a message, thank you!

end

Guess you like

Origin blog.csdn.net/qq_38693757/article/details/131439776