-
Notifications
You must be signed in to change notification settings - Fork 18
/
Program.cs
133 lines (106 loc) · 3.86 KB
/
Program.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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
using System.Reflection;
namespace Delegates;
class Program
{
static void Main()
{
}
public static int GetIndexOfFirstNonEmptyBin(int[] bins) =>
Array.FindIndex(bins, IsGreaterThanZero);
private static bool IsGreaterThanZero(int value) => value > 0;
// These examples illustrates how various types and members declared. Since these are all
// defined by .NET, we don't in fact need to define them ourselves, hence the #if false.
#if false
public static int FindIndex<T>(
T[] array,
Predicate<T> match)
public delegate bool Predicate<in T>(T obj);
public delegate void Action();
public delegate void Action<in T1>(T1 arg1);
public delegate void Action<in T1, in T2 >(T1 arg1, T2 arg2);
public delegate void Action<in T1, in T2, in T3>(T1 arg1, T2 arg2, T3 arg3);
public delegate TResult Func<out TResult>();
public delegate TResult Func<in T1, out TResult>(T1 arg1);
public delegate TResult Func<in T1, in T2, out TResult>(T1 arg1, T2 arg2);
public delegate TResult Func<in T1, in T2, in T3, out TResult>(
T1 arg1, T2 arg2, T3 arg3);
public Predicate(object target, IntPtr method);
public bool Invoke(T obj);
public IAsyncResult BeginInvoke(T obj, AsyncCallback callback, object state);
public bool EndInvoke(IAsyncResult result);
#endif
public static void CreatingADelegate()
{
var p = IsGreaterThanZero;
Console.WriteLine(p(42));
}
public static void ConstructingADelegate()
{
var p = new Predicate<int>(IsGreaterThanZero);
Console.WriteLine(p(42));
}
public static void ImplicitDelegateCreation()
{
Predicate<int> p = IsGreaterThanZero;
Console.WriteLine(p(42));
}
public static void ExplicitInstance()
{
var zeroThreshold = new ThresholdComparer { Threshold = 0 };
var tenThreshold = new ThresholdComparer { Threshold = 10 };
var hundredThreshold = new ThresholdComparer { Threshold = 100 };
Predicate<int> greaterThanZero = zeroThreshold.IsGreaterThan;
Predicate<int> greaterThanTen = tenThreshold.IsGreaterThan;
Predicate<int> greaterThanOneHundred = hundredThreshold.IsGreaterThan;
Console.WriteLine(greaterThanZero(42));
Console.WriteLine(greaterThanTen(42));
Console.WriteLine(greaterThanOneHundred(42));
Predicate<int> megaPredicate1 =
greaterThanZero + greaterThanTen + greaterThanOneHundred;
Predicate<int> megaPredicate2 = greaterThanZero;
megaPredicate2 += greaterThanTen;
megaPredicate2 += greaterThanOneHundred;
}
public static void UsingCreateDelegate()
{
var zeroThreshold = new ThresholdComparer { Threshold = 0 };
MethodInfo m = typeof(ThresholdComparer).GetMethod("IsGreaterThan")!;
var greaterThanZero = (Predicate<int>)m.CreateDelegate(
typeof(Predicate<int>), zeroThreshold);
Console.WriteLine(greaterThanZero(42));
}
public static void CallMeRightBack(Predicate<int> userCallback)
{
bool result = userCallback(42);
Console.WriteLine(result);
}
public static void TestForMajority(Predicate<int> userCallbacks)
{
int trueCount = 0;
int falseCount = 0;
foreach (Predicate<int> p in userCallbacks.GetInvocationList())
{
bool result = p(42);
if (result)
{
trueCount += 1;
}
else
{
falseCount += 1;
}
}
if (trueCount > falseCount)
{
Console.WriteLine("The majority returned true");
}
else if (falseCount > trueCount)
{
Console.WriteLine("The majority returned false");
}
else
{
Console.WriteLine("It's a tie");
}
}
}