Returns a subset of an array using a C # index

What is the best way to return a subset of a C # array specified as fromIndex and toIndex?

Obviously, I can use a loop, but are there any other approaches?

This is the signature of the method I want to fill out.

public static FixedSizeList<T> FromExisting(FixedSizeList<T> fixedSizeList, Int32 fromIndex, Int32 toIndex)

The internal implementation of FixedSizeList is

private T[] _Array;
this._Array = new T[size];
+3
source share
3 answers
myArray.Skip(fromIndex).Take(toIndex - fromIndex + 1);

EDIT: The result of Skip and Take is IEnumerable, and the counter will be zero until you use it.

if you try

        int[] myArray = {1, 2, 3, 4, 5};
        int[] subset = myArray.Skip(2).Take(2).ToArray();
Subset

will be {3, 4}

+9
source

The list already has a method CopyTothat should do what you want.

http://msdn.microsoft.com/en-us/library/3eb2b9x8.aspx

This is the Method Signature:

public void CopyTo( int index, T[] array, int arrayIndex,   int count )
+3
source

Array.Copywill do what you want .

+3
source

Source: https://habr.com/ru/post/1796721/


All Articles