//数组段ArraySegment<T>的使用
using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using System.Linq;
namespace ss
{
    class Program
    {
        static int SumOfSegments(ArraySegment<int>[] segments)
        {
            int sum = 0;
            foreach (var segment in segments)
            {
                for (int i = segment.Offset; i < segment.Offset + segment.Count; i++)
                {
                    sum += segment.Array[i];
                }
            }
            return sum;
        }
        static void Main()
        {
            int[] ar1 = { 1, 4, 5, 11, 13, 18 };
            int[] ar2 = { 3, 4, 5, 18, 21, 27, 33 };
            var segments = new ArraySegment<int>[2]
            {
                new ArraySegment<int>(ar1,0,3),
                new ArraySegment<int>(ar2,3,3)
            };
            var sum = Program.SumOfSegments(segments);
        }
    }
}


本文转载:CSDN博客