-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHelper.cs
109 lines (91 loc) · 2.59 KB
/
Helper.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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
using System;
using System.Collections.Generic;
using System.Linq;
namespace CSharpHelper
{
public static class Helper
{
public static TResult IfNotNull<TSource, TResult>(this TSource source, Func<TSource, TResult> action)
{
return Equals(source, default(TSource)) ? default(TResult) : action(source);
}
public static void IfNotNull<TSource>(this TSource source, Action<TSource> action)
{
if (Equals(source, default(TSource)))
return;
action(source);
}
public static bool IsInArray<T>(this T[] array, int value)
{
return value >= 0 && value < array.Length;
}
public static bool IsInArray<T>(this T[][] array, int x, int y)
{
if (x < 0 || x >= array.GetUpperBound(0))
return false;
bool result = true;
for (int i = 0; result && i < array.GetLength(0); i++)
result &= array[i].IsInArray(y);
return result;
}
public static bool IsInArray<T>(this T[,] array, int x, int y)
{
bool result = x >= 0 && y >= 0 && x < array.GetLength(0) && y < array.GetLength(1);
return result;
}
public static IList<Tresult> ToList<T, Tresult>(this IEnumerable<T> list, Func<T, Tresult> convert)
{
return list.Select(convert).ToList();
}
public static void ForEach<T>(this IEnumerable<IEnumerable<T>> array, Action<int, int, T> action)
{
int i = 0;
foreach (var items in array)
{
int i1 = i;
items.ForEach((j, item) => action(i1, j, item));
i++;
}
//for (int i = 0; i < array.Count; i++)
//{
// int i1 = i;
// array[i].ForEach((j, item) => action(i1, j, item));
//}
}
public static void ForEach<T>(this IEnumerable<T> array, Action<int, T> action)
{
int i = 0;
foreach (T item in array)
action(i++, item);
//for (int i = 0; i < array.Count; i++)
// action(i, array[i]);
}
public static void ForEach<T>(this IEnumerable<IEnumerable<T>> array, Action<T> action)
{
array.ForEach<IEnumerable<T>>(list => list.ForEach(action));
}
public static void ForEach<T>(this IEnumerable<T> array, Action<T> action)
{
if(action != null)
foreach (T item in array)
{
action(item);
//yield return item;
}
}
public static string Join<T>(this ICollection<T> array, string separator)
{
return array.Join(separator, item => item.ToString());
}
public static string Join<T>(this ICollection<T> array, string separator, Func<T, string> toString)
{
string result = "";
if (!array.Any())
return result;
result = toString(array.ElementAt(0));
for (int i = 1; i < array.Count; i++)
result += separator + toString(array.ElementAt(i));
return result;
}
}
}