C # Programming (34)----------array as a parameter

Source: Internet
Author: User

Original link: http://blog.csdn.net/shanyongxu/article/details/46765267

Array as a parameter

An array can be passed as a parameter to a method , or it can be returned from a method . To return an array , simply declare the array as the return type .

static void Main (string[] args)

{

Int[] Array = {1, 2, 3, 4, 5, 6};

PrintArray (New int[] {1, 2, 3, 4, 5, 6, 7});

PrintArray (array);

}

static void PrintArray (int[] arr)

{

foreach (var item in arr)

{

Console.WriteLine (item);

}

Console.readkey ();

}

Returns an array :

static void Main (string[] args)

{

int[] Array = GetArray (new int[] {1, 2, 3, 4, 5, 6, 7});

foreach (var item in array)

{

Console.WriteLine (item);

}

Console.readkey ();

}

static int [] GetArray (int[] arr)

{

return arr;

}

Array covariance

Arrays support covariance , which means that an array can be declared as a base class whose derived type elements can be assigned to an array element . For example , you can declare a object[] type parameter , passing it a person[];

Note : array covariance can only be used for speech and type , not for value types .

One problem with array covariance,It can only be resolved by a run-time exception.If you putPersonThe array is given an object array , and an object array can use any element derived from Object . For example , the compiler allows a string to be passed to an array element , but because the object array refers to the person array , a run-time exception occurs .

Arraysegment<t>

static void Main (string[] args)

{

String[] Array = {"Zhangsan", "Lisi", "Wangwu", "Zhaoliu"};

put the array in the arraysegment

arraysegment<string> arraystr = new arraysegment<string> (array);

can only be used for output , cannot use foreach

for (int i = Arraystr.offset; I < (arraystr.offset+arraystr.count); i++)

{

Console.WriteLine ("[{0}]: {1}", I,arraystr.array[i]);

}

Select a paragraph in the array , starting with the element labeled 1 , and taking 3 . The selected result is an element with a standard of

arraysegment<string> subarray = new Arraysegment<string> (array, 1, 3);

Console.readkey ();

}

Multiple ArraySegment instances can refer to the same original array , thus overlapping may occur . the Array property returns the entire original array . Instead of the copy of the array .

So use : arraystr.array[2] = "hahaha";

The array is changed to : Zhangsan Lisi hahaha Zhaoliu

Apply :arraysegment can be used in places where memory fragmentation is high ,,, Improve our program performance

For example, when the socket.beginreceive is received frequently, we do not know the buffer length to accept, so we set a fixed value beforehand, and this memory will be pinned by the pin. Waits until the network hardware receives the data to complete, this often is relatively easy to produce the fragment, if you use the WinDbg to look,! Dumpheap-type Free-stat will have a lot of objects but the value is 0. So you can use arraysegment+stack to solve

C # Programming (34)----------array as a parameter

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.