-
Notifications
You must be signed in to change notification settings - Fork 2k
Expand file tree
/
Copy pathdelegates.cs
More file actions
105 lines (74 loc) · 2.35 KB
/
Copy pathdelegates.cs
File metadata and controls
105 lines (74 loc) · 2.35 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
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
using System;
using System.IO;
using System.Collections.Generic;
namespace Delegates
{
delegate double FooDelegate(ref string param, out bool condition, params string[] args);
delegate int D1(int i, double d);
class A
{
public static int M1(int a, double b) { return (int)(a + b); }
}
class B
{
delegate int D2(int c, double d);
public static int M1(int f, double g) { return f - (int)g; }
public static void M2(int k, double l) { }
public static int M3(int g) { return -g + (+g); }
public static void M4(int g) { }
}
delegate bool Predicate<T>(T value);
public class X
{
public static bool F(int i) { return i < 2; }
public static bool G(string s) { return false; }
}
delegate void D(int x);
class C
{
public static void M1(int i) { }
public static void M2(int i) { }
public void M3(int i) { }
}
class Test
{
static void Main()
{
D cd1 = new D(C.M1); // instantiation with a static method
D cd2 = C.M2; // direct assignment
D cd3 = cd1 + cd2; // combination
D cd4 = cd3 + cd1;
D cd5 = cd4 - cd3; // removal
cd4 += cd5; // another style for combination
cd4 -= cd1; // another style for removal
C c = new C();
D cd6 = new D(c.M3); // instantiation with an instance method
D cd7 = new D(cd6); // instantiation with an existing delegate variable
cd1(-40); // invocation
int x = 0;
cd7(34 + x);
Predicate<int> pi = new Predicate<int>(X.F); // generic instantiation
Predicate<string> ps = X.G; // direct generic assignment
bool b = pi(3) & ps(""); // generic invocation
System.Threading.ContextCallback d; // assembly delegate
}
}
unsafe class E
{
Action<int> Field;
Action<int> Property { get; set; }
delegate*<int, void> FieldPtr;
delegate*<int, void> PropertyPtr { get; set; }
unsafe void M()
{
this.Field(0);
this.Property(0);
Field(0);
Property(0);
this.FieldPtr(0);
this.PropertyPtr(0);
FieldPtr(0);
PropertyPtr(0);
}
}
}