C # uses ref and out to pass arrays

Source: Internet
Author: User

C # uses ref and out to pass arrays

I. Passing an array using the ref parameter

The ref parameter of an array type must be explicitly assigned by the caller. Therefore, the recipient does not need to assign the value explicitly. The ref parameter of the receiver array type can modify the result of the caller array type. You can assign a null value to an array of the subject, or initialize it to another array. Please read the reference type parameter.

Example:

Initializes an array of arrays in the calling method (Main method) and passes it to the TheArray method using the ref parameter. Updates some array elements in the TheArray method, and then returns the array elements to the caller and displays them.

Using System;
Using System.Collections.Generic;
Using System.Linq;
Using System.Text;

Namespace Test
{
Class Program
{
static void TheArray (ref int[] arr)//receiver does not need to be explicitly assigned
{
if (arr = = null)//Create a new array as needed (not required)
{
arr = new INT[10]; Assigns an array to a null value, or initializes it to another array
}
Arr[0] = 11;
ARR[4] = 55;
}
static void Main (string[] args)
{
C # uses the ref parameter to pass an array-www.baike369.com
Int[] Array = {1, 2, 3, 4, 5};
TheArray (ref array); Using ref to pass an array, the caller definitely assigns a value
Console.Write ("array element is:");
for (int i = 0; i < array. Length; i++)
{
Console.Write (Array[i] + "");
}
Console.ReadLine ();
}
}
}

Operation Result:

Array elements are: 11 2 3 4 55

Second, passing an array using an out parameter
The callee must assign a value to an out parameter of the array type when it is used. Please read the output parameters.

Example:

Declare the array in the caller method (Main method), and initialize the array in the TheArray method. The array elements are then returned to the caller and displayed.

Using System;
Using System.Collections.Generic;
Using System.Linq;
Using System.Text;

Namespace Test
{
Class Program
{
static void TheArray (out int[] arr)
{
arr = new int[]{0,2,4,6,8}; Must be assigned a value
}
static void Main (string[] args)
{
Int[] Array;
TheArray (out array); Passing an array to the caller
Console.Write ("array element is:"); displaying array elements
for (int i = 0; i < array. Length; i++)
{
Console.Write (Array[i] + "");
}
Console.ReadLine ();
}
}
}

Operation Result:

Array elements are: 0 2 4 6 8

C # uses ref and out to pass arrays

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.