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

Source: Internet
Author: User

If ref or out is not used when declaring a parameter for a method, the parameter can have an associated value. The value can be changed in the method, but the changed value is not preserved when control is passed back to the calling procedure. You can change this behavior by using the method parameter keyword.

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 void Useparams (params object[] list)
{
for (int i = 0; i < list. Length; i++)
{
Console.WriteLine (List[i]);
}
}

static void Main ()
{
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] {A, ' a ', "keywords"};
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 (b, ' 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 void Useref (ref int i)
{
i + = 100;
Console.WriteLine ("I = {0}", i);
}

static void Main ()
{
int i = 10;

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

Useref (ref i);

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
*/
out

The Out keyword causes parameters to be passed by reference. This is similar to the REF keyword.

differs from ref:

    1. Ref requires that a variable be initialized before it is passed.
    2. Although a variable passed as an out parameter does not need to be initialized before it is passed, the method needs to be called to assign a value before the method returns.

Example :

As with the ref example, just change ref to out, and the variable I only needs to be declared.

static void Main ()
{
int i = 10; Switch
int i;
//
}

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

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.