bovender framework
C# framework that implements MVVM and more
ArrayExtensions.cs
1 /* Array.cs
2  * part of Bovender framework
3  *
4  * Copyright 2014-2018 Daniel Kraus
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */
18 using System;
19 using System.Collections.Generic;
20 using System.Linq;
21 using System.Text;
22 
23 namespace Bovender.Extensions
24 {
28  public static class ArrayExtensions
29  {
43  public static T[] Slice<T>(this T[] array, int start, int length)
44  {
45  if (length <= 0 || length > array.Length - start)
46  {
47  throw new ArgumentOutOfRangeException(
48  "length",
49  String.Format(
50  "Length of sliced array must be between 1 and {0}.",
51  array.Length - start
52  )
53  );
54  }
55 
56  if (start < 0 || start > array.Length - 1)
57  {
58  throw new ArgumentOutOfRangeException(
59  "start",
60  String.Format(
61  "Start index of sliced array is {0}; array has {1} elements.",
62  start, array.Length
63  )
64  );
65  }
66 
67  T[] slice = new T[length];
68  Array.Copy(array, start, slice, 0, length);
69  return slice;
70  }
71  }
72 }
Extensions methods for arrays.