-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathArrayUtil.cs
More file actions
59 lines (56 loc) · 1.62 KB
/
ArrayUtil.cs
File metadata and controls
59 lines (56 loc) · 1.62 KB
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
56
57
58
59
using System;
using System.Collections.Generic;
public static class ArrayUtil {
/// <summary>
/// Filter the given array into an array of a derived type
/// </summary>
/// <returns>
/// A new array that contains only the items in src that
/// are of the given derived type U
/// </returns>
/// <param name='src'>
/// Array to filter
/// </param>
/// <typeparam name='T'>
/// The item type of the array to filter
/// </typeparam>
/// <typeparam name='U'>
/// The type, derived from T, to filter the array to
/// </typeparam>
///
public static U[] Filter<T, U>(T[] src)
where U : T {
var res = new List<U>();
foreach (var item in src) {
if (item is U)
res.Add((U)item);
}
return res.ToArray();
}
/// <summary>
/// Filter the given array into a list of a derived type
/// </summary>
/// <returns>
/// A new list that contains only the items in src that
/// are of the given derived type U
/// </returns>
/// <param name='src'>
/// Array to filter
/// </param>
/// <typeparam name='T'>
/// The item type of the array to filter
/// </typeparam>
/// <typeparam name='U'>
/// The type, derived from T, to filter the array to
/// </typeparam>
///
public static List<U> FilterToList<T, U>(T[] src)
where U : T {
var res = new List<U>();
foreach (var item in src) {
if (item is U)
res.Add((U)item);
}
return res;
}
}