c# - Create an array that points to only part of another array? -


i have huge array contains reference type elements, , want create lot of other arrays point specific parts of 1 big array.

in other words, want create "indexers" or "pointers lengths".

in c++ it's easy using pointers , each pointer assign length, example create struct contains pointer length.

how can achieve in c#/.net?

the whole point avoid copying anything, want pointers specific parts in array exists in memory.

any ideas?

jon's suggestion of using arraysegment<t> want. if wanting represent pointer interior of array, way can in c++, here's code that. no warranty expressed or implied, use @ own risk.

this code not track "length" of interior pointer in way, quite easy add feature if want.

internal struct arrayptr<t> {   public static arrayptr<t> null { { return default(arrayptr<t>); } }   private readonly t[] source;   private readonly int index;    private arrayptr(arrayptr<t> old, int delta)   {     this.source = old.source;     this.index = old.index + delta;     debug.assert(index >= 0);     debug.assert(index == 0 || this.source != null && index < this.source.length);   }    public arrayptr(t[] source)   {     this.source = source;     index = 0;   }    public bool isnull()   {     return this.source == null;   }    public static bool operator <(arrayptr<t> a, arrayptr<t> b)   {     debug.assert(object.referenceequals(a.source, b.source));     return a.index < b.index;   }    public static bool operator >(arrayptr<t> a, arrayptr<t> b)   {     debug.assert(object.referenceequals(a.source, b.source));     return a.index > b.index;   }    public static bool operator <=(arrayptr<t> a, arrayptr<t> b)   {     debug.assert(object.referenceequals(a.source, b.source));     return a.index <= b.index;   }    public static bool operator >=(arrayptr<t> a, arrayptr<t> b)   {     debug.assert(object.referenceequals(a.source, b.source));     return a.index >= b.index;   }    public static int operator -(arrayptr<t> a, arrayptr<t> b)   {     debug.assert(object.referenceequals(a.source, b.source));     return a.index - b.index;   }    public static arrayptr<t> operator +(arrayptr<t> a, int count)   {     return new arrayptr<t>(a, +count);   }    public static arrayptr<t> operator -(arrayptr<t> a, int count)   {     return new arrayptr<t>(a, -count);   }    public static arrayptr<t> operator ++(arrayptr<t> a)   {     return + 1;   }    public static arrayptr<t> operator --(arrayptr<t> a)   {     return - 1;   }    public static implicit operator arrayptr<t>(t[] x)   {     return new arrayptr<t>(x);   }    public static bool operator ==(arrayptr<t> x, arrayptr<t> y)   {     return x.source == y.source && x.index == y.index;   }    public static bool operator !=(arrayptr<t> x, arrayptr<t> y)   {     return !(x == y);   }    public override bool equals(object x)   {     if (x == null) return this.source == null;     var ptr = x arrayptr<t>?;     if (!ptr.hasvalue) return false;     return == ptr.value;   }    public override int gethashcode()   {     unchecked     {       int hash = this.source == null ? 0 : this.source.gethashcode();       return hash + this.index;     }   }    public t this[int index]   {     { return source[index + this.index]; }     set { source[index + this.index] = value; }   } } 

now can stuff like:

double[] arr = new double[10]; var p0 = (arrayptr<double>)arr; var p5 = p0 + 5; p5[0] = 123.4; // sets arr[5] 123.4 var p7 = p0 + 7; int diff = p7 - p5; // 2 

Comments