以下代码利用ArrarSegment泛型结构,从int数组arr中取出arr[2]到arr[5]
1using System;
2using System.Collections;
3
4
5class Program
6{
7 static void Main(string[] args)
8 {
9
10 int[] arr = { 1, 2, 3, 4, 5, 6, 7, 8 };
11 ArraySegment<int> segment = new ArraySegment<int>(arr, 2, 3);
12
13 for (int i = segment.Offset; i < segment.Offset+segment.Count; i++)
14 {
15 Console.WriteLine(segment.Array[i]);
16 }
17
18 Console.Read();
19 }
20}
运行结果:
3
4
5