How to get sub array without cloning

前端 未结 3 1199
借酒劲吻你
借酒劲吻你 2020-12-18 09:19

I know in C# we can always get the sub-array of a given array by using Array.Copy() method. However, this will consume more memory and processing time which is

3条回答
  •  鱼传尺愫
    2020-12-18 10:00

    You might be interested in ArraySegments or unsafe.


    ArraySegments delimits a section of a one-dimensional array.

    Check ArraySegments in action

    ArraySegments usage example:

     int[] array = { 10, 20, 30 };
    
     ArraySegment segment = new ArraySegment(array, 1, 2);
     // The segment contains offset = 1, count = 2 and range = { 20, 30 }
    

    Unsafe define an unsafe context in which pointers can be used.

    Unsafe usage example:

        int[] a = { 4, 5, 6, 7, 8 };
    
        unsafe
        {
            fixed (int* c = a)
            {
                // use the pointer
            }
        }
    

提交回复
热议问题