The difference between params, ref, out in C #

Source: Internet
Author: User

params

The params keyword can specify a method parameter that takes a parameter at a variable number of arguments.

    1. No additional arguments are allowed after the params keyword in the method declaration, and only one params keyword is allowed in the method declaration.

Example :

The literal meaning is rather difficult to understand, so it is useful to look at examples.

// Keywords_params.cs

using System;

class App
{
Public Static voidUseparams (params Object[] list)
{
for (intI= 0; I<list. Length; I++)
{
Console.WriteLine (List[i]);
}
}

Static voidMain ()
{
//It is common practice to construct an array of objects and then use this array as a parameter to the method
        Object[] Arr= New Object[3]  { keywords, 'a', ' " };
Useparams (arr);

//with the params modification method parameter, we can use a set of objects directly as parameters
//of course, this set of parameters needs to conform to the calling method of the parameter requirements
Useparams ( -, 'a', "keywords");

Console.read ();
}
}
ref

The ref keyword enables parameters to be passed by reference. The effect is that when control is passed back to the calling method, any changes made to the parameter in the method are reflected in the variable.

    1. To use the ref parameter, both the method definition and the calling method must explicitly use the ref keyword.
    2. The arguments passed to the ref parameter must be initialized first. This differs from out in that the out parameter does not need to be explicitly initialized before it is passed.
    3. property is not a variable and therefore cannot be passed as a ref parameter.
    4. Although ref and out are handled differently at run time, they are handled identically at compile time. Therefore, if one method takes a ref parameter and the other method takes an out parameter, the two methods cannot be overloaded. For example, from a compilation point of view, the two methods in the following code are identical. Attempting to do so will result in the code not being compiled.
    5. If a method takes a ref or out parameter, and the other method does not take these two types of arguments, it can be overloaded.

Example :

Passing value types by reference is useful, but ref is also useful for passing reference types. This allows the called method to modify the object referenced by the reference, because the reference itself is passed by reference.

// Keywords_ref.cs

using System;

class App
{
Public Static voidUseref (ref inti)
{
I + = + ;
Console.WriteLine ("i = {0}", I);
}

Static voidMain ()
{
intI= Ten;

//to view the value before calling a method
Console.WriteLine ("before the method calling:i = {0}", i);

Useref (refi);

//view values after calling a method
Console.WriteLine ("After the method calling:i = {0}", i);
Console.read ();
}
}

/**/ /*
Console output:
Before the method calling:i = 10
i = 110
After the method calling:i = 110
*/

C # ref vs out Difference

In C #, parameters can be passed either by value or by reference. Passing parameters by reference allows a function member to change the value of a parameter and keep the change. To pass parameters by reference, you can use the ref or out keyword. The two keywords, ref and out, can provide similar effects and act like pointer variables in C. The difference between them is:

1. When using a ref parameter, the passed-in parameter must first be initialized. For out, initialization must be done in the method.

2. When using ref and out, the ref or out keyword is added to the method's parameters and execution methods. To meet the match.

3. Out is suitable for use where multiple return values need to be retrun, while ref is used to modify the caller's reference in a method that needs to be called.

Note: In C #, the parameters of a method are passed in four types: Pass value (by value), address (by reference), output parameter (by output), array parameter (by array). The pass-through parameter requires no additional modifiers, the address parameter needs a modifier ref, the output parameter requires a modifier out, and the array parameter requires a modifier params. If the value of the parameter is changed during a method call, the parameters of the passed-in method do not change after the method call is complete, but instead retain the value of the original pass-in. On the contrary, if the method invocation procedure changes the value of the parameter, the parameters of the incoming method are changed after the call is completed. In fact, from the name we can clearly see the meaning of the two--the pass parameter is passed a copy of the invocation parameter, and the address parameter is passed the memory of the calling parameter, which is the same storage location inside and outside the method.

The ref method parameter keyword on the method parameter causes the method reference to be passed to the same variable of the method. When control is passed back to the calling method, any changes made to the parameter in the method are reflected in the variable.

To use the ref parameter, you must explicitly pass the parameter to the method as a ref parameter. The value of the ref parameter is passed to the ref parameter.

The arguments passed to the ref parameter must be initialized first. Comparing this method to an out parameter, the latter's arguments do not have to be explicitly initialized before they are passed to the out parameter.

property is not a variable and cannot be passed as a ref parameter.

If the declarations of both methods differ only in their use of ref, an overload occurs. However, you cannot define overloads that differ only in respect of ref and out.

Out

The Out method parameter keyword on the method parameter causes the method reference to be passed to the same variable of the method. When control is passed back to the calling method, any changes made to the parameter in the method are reflected in the variable.

Declaring an out method is useful when you want a method to return more than one value. Methods that use out parameters can still return a value. A method can have more than one out parameter.

To use an out parameter, you must explicitly pass the parameter to the method as an out parameter. The value of the out parameter is not passed to the out parameter.

You do not have to initialize a variable passed as an out parameter. However, you must assign a value to the out parameter before the method returns.

property is not a variable and cannot be passed as an out parameter

The difference between params, ref, out in C #

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.