參考來源: https://www.dotnetperls.com/array-slice

 

用過其他能矩陣式運算的程式語言都對Slice不陌生,因此一個方便的好方法就應該得到廣傳

public static class Extensions
{
    /// <summary>
    /// Get the array slice between the two indexes.
    /// ... Inclusive for start index, exclusive for end index.
    /// </summary>
    public static T[] Slice<T>(this T[] source, int start, int end)
    {
        // Handles negative ends.
        if (end < 0)
        {
            end = source.Length + end;
        }
        int len = end - start;

        // Return new array.
        T[] res = new T[len];
        for (int i = 0; i < len; i++)
        {
            res[i] = source[i + start];
        }
        return res;
    }
}
文章標籤
全站熱搜
創作者介紹
創作者 wings890109 的頭像
wings890109

幽嵐飋翼

wings890109 發表在 痞客邦 留言(0) 人氣(376)