Array section in C #

Source: Internet
Author: User

1. Description of array segments
<1>. The ArraySegment structure <T> indicates an array segment. If a method returns a part of the array, or
If a method is used to pass a part of an array, an array segment can be used. Three parameters can be passed through ArraySegment <T>
(Array, array segment start position, number of elements selected from the start position), you can also pass only one parameter

<2>. The Array segment does not copy the elements of the original Array, but the original Array can be accessed through the Array attribute in ArraySegment <T>,
If the elements in the array segment change, these changes will be reflected in the original array.

 

2. An example

Private int SumOfSegments (ArraySegment <int> [] segments)
{
Int sum = 0;
Foreach (ArraySegment <int> segment in segments)
// Store arrays of array segments cyclically
{
For (int I = segment. Offset; I <segment. Offset +
Segment. Count; I ++)
// Process the array segment. Offset is the starting position in the element array.
// Count indicates the number of entries to be extracted.
// Arary is the original array.
{
Sum + = segment. Array [I];
// Calculate the sum of Elements
}
}
Return sum;
}

----------- Call ------------------------

Private void button#click (object sender, EventArgs e)
{
Int [] arr1 = new int };
Int [] arr2 = new int [] {, 33 };
// Define the array www.2cto.com
Var segments = new ArraySegment <int> [2]
{
New ArraySegment <int> (arr1, 0, 3 ),
New ArraySegment <int> (arr2, 3, 3)
};
Var sum = SumOfSegments (segments );
MessageBox. Show (sum. ToString ());
}


Author: limlimlim

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.