CREATE NEW REFERENCE TO ARRAY WITH DIFFERENT LENGTH

lois waisbrooker

Total Post:70

Points:494
Posted by  lois waisbrooker
C# 
C#
 918  View(s)
Ratings:
Rate this:

Given some array:

int[] array = new int[8000];

Is it possible to refer to a new array such that:

int[] array2 = Array.SameReferenceDifferentLength(array, 4000);
  1. F Scott Fitzgerald

    Post:29

    Points:203
    Re: Create new reference to array with different length

    var a = new string[] { "a", "b", "c", "d", "e" };
     
    var b = new ArraySegment<string>(a, 1, 3);
     
    foreach (var s in b)
    {
        Console.WriteLine(s);
    }

    This makes a shallow reference to the specified range of the array. That is, it doesn't copy the array data.

    If you want to index an ArraySegment<T>, you can cast it to a IList<T> then use the indexer provided by that interface.

     

Answer

NEWSLETTER

Enter your email address here always to be updated. We promise not to spam!