ArraySegmentSample

时间:2023-12-13 09:00:38
    class Program
{
static void Main()
{
int[] ar1 = { , , , , , };
int[] ar2 = { , , , , , , };
var segments = new ArraySegment<int>[]
{
new ArraySegment<int>(ar1, , ),
new ArraySegment<int>(ar2, , )
}; var sum = SumOfSegments(segments);
Console.WriteLine("sum of all segments: {0}", sum);
Console.ReadKey(); } static int SumOfSegments(ArraySegment<int>[] segments)
{
int sum = ;
foreach (var segment in segments)
{
for (int i = segment.Offset; i < segment.Offset + segment.Count; i++)
{
sum += segment.Array[i];
} }
return sum;
}
}

相关文章