A brief introduction to the out modifier, ref modifier, and params modifier for C #

Source: Internet
Author: User

One, out modifier

1. Calling a method with an output parameter also requires an out modifier, but local variables passed as output variables do not need to be assigned before they are passed as output variables (because they are changed or lost after the call), and the compiler allows you to pass the unassigned data because it is within the called method The department must contain data allocations for such variables.

2, a useful use, is to call a method to return multiple return values

static void Add (out int x, out int y, out int ans)
{
x = 1;
y = 2;
Ans = x + y;
}

3, note that the output parameter is defined, you must assign a valid value to the parameter before exiting the method, or it will cause a compiler error.

Second, ref modifier

This illustrates the main difference between out and ref;

1. If you want a method to operate on different data declared in the caller's scope (usually by changing its value), you must use a reference parameter.

2.ref reference parameters must be initialized before they are passed to the method , because they are passed a reference to an existing variable, and if they are not assigned a value, it is equivalent to manipulating an unassigned local variable.

Third, params modifier

The params keyword can pass a variable number of arguments (the same type) as a single logical parameter to the method. In other words, create a function calculateaverage (), which allows the caller to pass in any number of arguments and return the calculated average. If we define a params to receive the double[] data type, the caller only needs to pass in a comma-delimited double list: The NET runtime automatically wraps this set of double into an array of type double in the background.

static void Calculateaverage (params double[] valus) {}//method

Invocation type: Double Average=calculateaverage (4.0,3.3,4.2,5.6);//comma-separated argument list of type Double.

Double average=calculateaverage (data);//data is an array, double[] data={4.0,3.3,4.2,5.6}.

Calculateaverage ();//null value call.

A brief introduction to the out modifier, ref modifier, and params modifier for 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.