Params, ref, out keyword

Source: Internet
Author: User

Parameter array Params:
C # Allow you to specify one (only one) specific parameter for a function. This parameter must be the last parameter in the function definition. It is called a parameter.

Array. A parameter array can call a function with an indefinite number of parameters, which can be defined using the Params keyword.

features
1. Used in the parameter list of method members, this method provides the ability to change the number of parameters
2, this parameter must be a one-dimensional array
3. The Params keyword can specify that the parameter method parameter is used when the number of parameters is variable.
4. No other parameters are allowed after the Params keyword in the method declaration, and only one Params keyword is allowed in the method declaration.
using system;
using system. collections. generic;
using system. text;
namespace consoleapplication5
{< br> class Program
{< br> static void main (string [] ARGs)
{< br> int [] intarr = {1, 2, 3, 4, 5 };
useparams ("hello", intarr );
useparams ("world", 6, 7, 8);
console. readline ();
}

Static void useparams (string strmsg, Params int [] list)
{
Console. writeline (strmsg );
For (INT I = 0; I <list. length; I ++)
{
Console. Write (list [I] + "");
}
Console. writeline ();
}

}
}
Output result:
Hello
1 2 3 4 5
World
6 7 8

Reference parameter ref output parameter out
In C #, you can pass parameters either by value or by reference. PASS Parameters by referencing, and allow the Members in the method to change the parameter value,

And keep the changes. To pass parameters through references, you can use the ref and out keywords. The ref and out functions are equivalent to pointers in C.
Both ref and out allow parameters to be passed by reference, but there are some differences between them.
1. The ref parameter must be initialized first, but the out parameter does not need to be initialized first, but must be initialized in the method.
2. Out is suitable for use where multiple retrun return values are required, while ref is used when the caller's reference is modified by the method to be called.
3. When using ref and out, you must use the ref or out parameter before using and defining methods.

 

Ref
The ref keyword allows the parameter to be passed by reference. The effect is that when the control is passed back to the call method, any changes made to the parameter in the method will

Reflected in this variable.

To use the ref parameter, you must explicitly use the ref keyword for both method definition and call methods.
Parameters passed to the ref parameter must be initialized first. Unlike out, out parameters do not need to be explicitly initialized before being passed.
Attributes are not variables, so they cannot be passed as REF parameters.
While ref and out are processed differently at runtime, they are processed in the same way during compilation. Therefore, if a method uses

The ref parameter, while the other method uses the out parameter, the two methods cannot be reloaded. For example, from the compilation perspective, the followingCodeIn

Methods are identical. If you try to do so, the Code cannot be compiled.
If one method uses the ref or out parameter, and the other method does not use the two parameters, you can perform the overload.
Example:

It is useful to pass value types by reference, but ref is also useful for passing reference types. This allows the called method to modify the reference

Because the reference itself is passed by reference.

// Keywords_ref.cs

Using system;

Class app
{
Public static void useref (ref int I)
{
I + = 100;
Console. writeline ("I = {0}", I );
}

Static void main ()
{
Int I = 10;

// View the value before calling the Method
Console. writeline ("before the method calling: I = {0}", I );

Useref (Ref I );

// View the value after the method is called
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
*/

 

Out
The out keyword causes the parameter to be passed through reference. This is similar to the ref keyword.

Differences from Ref:

Ref requires that the variables be initialized before being passed.
Although the variables passed as the out parameter do not need to be initialized before being passed, you need to call a method to assign values before the method returns.
Example:

Different from the ref example, you only need to change the ref to out, and then the variable I only needs to be declared.

Static void main ()
{
// Int I = 10; changed
Int I;
//
}

 

 

C # differences between ref and out
In C #, parameters can be passed either by value or by reference. Pass a parameter through reference to allow function members to change the parameter value and keep the Parameter

Change. To pass parameters through references, you can use the ref or out keyword. Both the ref and out keywords can provide similar functions and functions.

It is similar to the pointer variable in C. Their differences are:

1. When using ref parameters, the input parameters must be initialized first. For out, initialization must be completed in the method.

2. When using ref and out, the ref or out keyword must be added to both the method parameters and the execution method. To match.

3,Out is suitable for use where multiple retrun return values are required, while ref is used to modify the reference of the caller in the method to be called.

 

Note: in C #, there are four types of method parameters passed: by value, by reference, and output parameter (by output

), Array parameter (by array ). There is no additional modifier for the value passing parameter, the address passing parameter requires the modifier ref, and the output parameter requires the modifier out.

, The array parameter must be modifier Params. If the value of the parameter is changed during a method call

After the call is completed, the original value is retained. The address transfer parameter is the opposite. If the parameter is changed during the method call process

Then the parameters of the input method change after the call is completed. In fact, we can clearly see the meaning of the two from the name -- pass

The value parameter transmits a copy of the call parameter, while the transfer parameter transmits the memory address of the call parameter. The parameter points

The same storage location.

The ref method parameter keyword in the method parameter allows the method reference to pass to the same variable of the method. When the control is passed back to the call method

Any changes made to the parameter will be reflected in this variable.

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

Parameters passed to the ref parameter must be initialized first. Compared with the out parameter, the latter parameter does not need to be passed to the out parameter.

Explicit initialization.

Attributes are not variables and cannot be passed as REF parameters.

If the declarations of the two methods are only different in their use of ref, an overload will occur. However, it cannot be defined only in the ref and out aspects.

Different loads.

Out

The out method parameter keyword on the method parameter enables the method reference to pass to the same variable of the method. When the control is passed back to the call method

Any changes made to the parameter will be reflected in this variable.

When you want the method to return multiple values, it is very useful to declare the out method. You can still return a value using the out parameter. One method can be

With more than one out parameter.

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

You do not need to initialize the variable passed as the out parameter. However, the out parameter must be assigned a value before the method is returned.

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

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.