Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Starting from <a href="https://stackoverflow.com/questions/5756692/arraysegment-returning-the-actual-segment-c/5756714#5756714">Thomas Levesque's suggestion</a> I've built a simple <code>ArraySegmentWrapper&lt;T&gt;</code> class to use in this way:</p> <pre><code>static void Main(string[] args) { int[] arr = new int[10]; for (int i = 0; i &lt; arr.Length; i++) arr[i] = i; // arr = 0,1,2,3,4,5,6,7,8,9 var segment = new ArraySegmentWrapper&lt;int&gt;(arr, 2, 7); segment[0] = -1; segment[6] = -1; // now arr = 0,1,-1,3,4,5,6,7,-1,9 // this prints: -1,3,4,5,6,7,-1 foreach (var el in segment) Console.WriteLine(el); } </code></pre> <p><strong>Implementation:</strong></p> <pre><code>public class ArraySegmentWrapper&lt;T&gt; : IList&lt;T&gt; { private readonly ArraySegment&lt;T&gt; segment; public ArraySegmentWrapper(ArraySegment&lt;T&gt; segment) { this.segment = segment; } public ArraySegmentWrapper(T[] array, int offset, int count) : this(new ArraySegment&lt;T&gt;(array, offset, count)) { } public int IndexOf(T item) { for (int i = segment.Offset; i &lt; segment.Offset + segment.Count; i++) if (Equals(segment.Array[i], item)) return i; return -1; } public void Insert(int index, T item) { throw new NotSupportedException(); } public void RemoveAt(int index) { throw new NotSupportedException(); } public T this[int index] { get { if (index &gt;= this.Count) throw new IndexOutOfRangeException(); return this.segment.Array[index + this.segment.Offset]; } set { if (index &gt;= this.Count) throw new IndexOutOfRangeException(); this.segment.Array[index + this.segment.Offset] = value; } } public void Add(T item) { throw new NotSupportedException(); } public void Clear() { throw new NotSupportedException(); } public bool Contains(T item) { return this.IndexOf(item) != -1; } public void CopyTo(T[] array, int arrayIndex) { for (int i = segment.Offset; i &lt; segment.Offset + segment.Count; i++) { array[arrayIndex] = segment.Array[i]; arrayIndex++; } } public int Count { get { return this.segment.Count; } } public bool IsReadOnly { get { return false; } } public bool Remove(T item) { throw new NotSupportedException(); } public IEnumerator&lt;T&gt; GetEnumerator() { for (int i = segment.Offset; i &lt; segment.Offset + segment.Count; i++) yield return segment.Array[i]; } System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator() { return GetEnumerator(); } } </code></pre> <hr> <h2>EDIT :</h2> <p>As pointed out by @JeppeStigNielsen in the comments, since .NET 4.5 <code>ArraySegment&lt;T&gt;</code> implements <code>IList&lt;T&gt;</code></p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload