Created
March 27, 2014 10:28
-
-
Save devlights/9804559 to your computer and use it in GitHub Desktop.
System.ArraySegment<T>
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
namespace ArraySegmentSample | |
{ | |
using System; | |
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
var orig = new[] { 1, 2, 3, 4, 5 }; | |
var segment = new ArraySegment<int>(orig, 1, 3); | |
// | |
// .NET 4.0までのやり方 | |
// 4.0までは ArraySegment<T>はコレクションインターフェースを | |
// まったく実装していない。 | |
// | |
for (var i = 0; i < segment.Count; i++) | |
{ | |
Console.WriteLine(segment.Array[segment.Offset + i]); | |
} | |
for (var i = segment.Offset; i < (segment.Offset + segment.Count); i++) | |
{ | |
Console.WriteLine(segment.Array[i]); | |
} | |
// | |
// .NET 4.5からのやり方 | |
// 4.5から、ArraySegment<T>の宣言部分が以下のように変わった。 | |
// public struct ArraySegment<T> : IList<T>, | |
// ICollection<T>, | |
// IReadOnlyList<T>, | |
// IReadOnlyCollection<T>, | |
// IEnumerable<T>, | |
// IEnumerable | |
// | |
//var list = segment as IList<int>; | |
//foreach (var item in list) | |
//{ | |
// Console.WriteLine(item); | |
//} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment