C # reference method parameter keywords: params, ref, and out

Source: Internet
Author: User

If you do not use ref or out when declaring a parameter for a method, the parameter can have an associated value. This value can be changed in the method, but the changed value is not retained when the control is passed back to the call process. You can change this behavior by using the method parameter keyword.

Params
The params keyword specifies that the parameter method parameter is used when the number of parameters is variable.

After the params keyword in the method declaration, no other parameters are allowed, and only one params keyword is allowed in the method declaration.
Example:

The literal meaning is difficult to understand, so it is useful to look at the example.

// Keywords_params.cs

Using System;

Class App
{
Public static void UseParams (params object [] list)
{
For (int I = 0; I <list. Length; I ++)
{
Console. WriteLine (list [I]);
}
}

Static void Main ()
{
// The general practice is to first construct an object array and then use this array as a parameter of the method.
Object [] arr = new object [3] {100, a, "keywords "};
UseParams (arr );

// After using the params modifier parameter, we can directly use a group of objects as parameters
// Of course, this set of parameters must meet the parameter requirements of the called Method
UseParams (100, a, "keywords ");

Console. Read ();
}
}
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 parameters in the method will be reflected in the 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 one 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 two methods in the following code are exactly the same. 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 object referenced by 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;
//
 

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.