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