Asp.net array ref and out pass the array

Source: Internet
Author: User
Tags arrays

Asp tutorial. net array ref and out passing arrays

Like all out parameters, an out parameter of the array type must be assigned a value before being used, that is, it must be assigned a value by the called party.

Class testarraysclass
{
Static void main ()
 {
// Declare and initialize an array
Int [,] thearray = new int [5, 10];
System. console. writeline ("the array has {0} dimensions", thearray. rank );
  }
 }
// Output: the array has 2 dimensions


The array is actually the object [all objects in. net are derived from object objects], not just the addressable continuous memory areas like in c and c ++. Array is the abstract base type of all arrays. You can use array to have attributes and other members. For example, you can use the length attribute to obtain the length of an array:

Int [] number = {1, 2, 3, 4}
Int lengthofnumbers = number. length;


Use ref and out to pass the array
. For example:

Static void testmethod1 (out int [] arr)
{
// Definite assignment of array
Arr = new int [10];
    }

Like all ref parameters, the ref parameter of the array type must be explicitly assigned by the caller. Therefore, it is not necessary for the receiver to explicitly assign values. You can change the ref parameter of the array type to the result of the call.
For example, you can assign a null value to an array or initialize it as another array. For example:

Static void testmethod2 (ref int [] arr)
{
// Arr initalized to a different array
Arr = new int [10];
    }

   
Below we use two examples to illustrate the usage difference between out and ref when passing an array to a method.
In this example, declare the array thearray in the caller (main method) and initialize the array in the fillarray method. Then, the array elements are returned to the caller and displayed.

Class testout
{
Static void fillarray (out int [] arr)
    {
Arr = new int [5] {1, 2, 3, 4, 5 };
        }
Static void main ()
    {
Int [] thearray;
Fillarray (out thearray );
System. console. writeline ("array elements are :");
For (int I = 0; I <thearray. length; I ++)
        {
System. console. write (thearray [I] + "");
            }
System. console. writeline ("press any key to exit .");
        }
    }

In the following example, initialize the array thearray in the caller (main method) and pass it to the fillarray method by using the ref parameter. Update some array elements in the fillarray method. Then, the array elements are returned and called and displayed.

Class testref
{
Static void fillarray (ref int [] arr)
    {
Int (arr = null)
Arr = new int [10];
        }
Arr [0] = 1111;
Arr [4] = 5555;
    }
  
Static void main ()
    {
Int [] thearray = {1, 2, 3, 4, 5 };
Fillarray (ref thearray );
System. console. writeline ("array elements are :");
For (int I = 0; I <thearray. length; I ++)
        {
System. console. write (thearray [I] + "");
        }

        }

       
PS Tutorial: understand the differences between out and ref.

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.