-
Notifications
You must be signed in to change notification settings - Fork 8
/
Program.cs
104 lines (77 loc) · 3.13 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
using System.Reflection;
namespace Delegates;
class Program
{
static void Main()
{
}
public static int GetIndexOfFirstNonEmptyBin(int[] bins)
=> Array.FindIndex(bins, IsNonZero);
private static bool IsNonZero(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 = IsNonZero;
Console.WriteLine(p(42));
}
public static void ConstructingADelegate()
{
var p = new Predicate<int>(IsNonZero);
Console.WriteLine(p(42));
}
public static void ImplicitDelegateCreation()
{
Predicate<int> p = IsNonZero;
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 = m.CreateDelegate<Predicate<int>>(zeroThreshold);
Console.WriteLine(greaterThanZero(42));
}
public static void CallMeRightBack(Predicate<int> userCallback)
{
bool result = userCallback(42);
Console.WriteLine(result);
}
}
public delegate void Log(string message, string? source = "");