-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathIEnumerableExtensions.cs
55 lines (45 loc) · 1.65 KB
/
IEnumerableExtensions.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
using System;
using System.Collections.Generic;
namespace UEx
{
public static class IEnumerableExtensions
{
public static T MaxElement<T, TCompare>(this IEnumerable<T> collection, Func<T, TCompare> func)
where TCompare : IComparable<TCompare>
{
T maxItem = default(T);
TCompare maxValue = default(TCompare);
if (collection == null)
return maxItem;
foreach (var item in collection)
{
TCompare temp = func(item);
if (maxItem == null || temp.CompareTo(maxValue) > 0)
{
maxValue = temp;
maxItem = item;
}
}
return maxItem;
}
public static T[] RemoveRange<T>(this T[] array, int index, int count)
{
if (count < 0)
throw new ArgumentOutOfRangeException("count", " is out of range");
if (index < 0 || index > array.Length - 1)
throw new ArgumentOutOfRangeException("index", " is out of range");
if (array.Length - count - index < 0)
throw new ArgumentException("index and count do not denote a valid range of elements in the array", "");
var newArray = new T[array.Length - count];
for (int i = 0, ni = 0; i < array.Length; i++)
{
if (i < index || i >= index + count)
{
newArray[ni] = array[i];
ni++;
}
}
return newArray;
}
}
}