Skip to content

Commit 7eace65

Browse files
pkulikovBillWagner
authored andcommitted
Updated delegate addition and removal examples (#945)
1 parent e70b545 commit 7eace65

File tree

2 files changed

+55
-19
lines changed

2 files changed

+55
-19
lines changed

csharp/language-reference/operators/SubtractionOperator.cs

Lines changed: 40 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ public static class SubtractionOperator
77
public static void Examples()
88
{
99
DelegateRemoval();
10+
DelegateRemovalNoChange();
11+
DelegateRemovalAndNull();
1012
SubtractAndAssign();
1113
}
1214

@@ -25,14 +27,49 @@ private static void DelegateRemoval()
2527
abba(); // output: abba
2628
Console.WriteLine();
2729

30+
var nihil = abbaab - abbaab;
31+
Console.WriteLine(nihil is null); // output: True
32+
// </SnippetDelegateRemoval>
33+
}
34+
35+
private static void DelegateRemovalNoChange()
36+
{
37+
// <SnippetDelegateRemovalNoChange>
38+
Action a = () => Console.Write("a");
39+
Action b = () => Console.Write("b");
40+
41+
var abbaab = a + b + b + a + a + b;
2842
var aba = a + b + a;
43+
2944
var first = abbaab - aba;
3045
first(); // output: abbaab
3146
Console.WriteLine();
47+
Console.WriteLine(object.ReferenceEquals(abbaab, first)); // output: True
3248

33-
var nihil = abbaab - abbaab;
34-
Console.WriteLine(nihil is null); // output: True
35-
// </SnippetDelegateRemoval>
49+
Action a2 = () => Console.Write("a");
50+
var changed = aba - a;
51+
changed(); // output: ab
52+
Console.WriteLine();
53+
var unchanged = aba - a2;
54+
unchanged(); // output: aba
55+
Console.WriteLine();
56+
Console.WriteLine(object.ReferenceEquals(aba, unchanged)); // output: True
57+
// </SnippetDelegateRemovalNoChange>
58+
}
59+
60+
private static void DelegateRemovalAndNull()
61+
{
62+
// <SnippetDelegateRemovalAndNull>
63+
Action a = () => Console.Write("a");
64+
65+
var nothing = null - a;
66+
Console.WriteLine(nothing is null); // output: True
67+
68+
var first = a - null;
69+
a(); // output: a
70+
Console.WriteLine();
71+
Console.WriteLine(object.ReferenceEquals(first, a)); // output: True
72+
// </SnippetDelegateRemovalAndNull>
3673
}
3774

3875
private static void SubtractAndAssign()

snippets/csharp/language-reference/operators/AdditionExamples.cs

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -41,36 +41,35 @@ private static void StringConcatenation()
4141
private static void AddDelegates()
4242
{
4343
// <SnippetAddDelegates>
44-
Action<int> printDouble = (int s) => Console.WriteLine(2 * s);
45-
Action<int> printTriple = (int s) => Console.WriteLine(3 * s);
46-
Action<int> combined = printDouble + printTriple;
47-
combined(5);
48-
// Output:
49-
// 10
50-
// 15
44+
Action a = () => Console.Write("a");
45+
Action b = () => Console.Write("b");
46+
Action ab = a + b;
47+
ab(); // output: ab
5148
// </SnippetAddDelegates>
49+
Console.WriteLine();
5250
}
5351

5452
private static void AddAndAssign()
5553
{
5654
// <SnippetAddAndAssign>
57-
int a = 5;
58-
a += 9;
59-
Console.WriteLine(a);
55+
int i = 5;
56+
i += 9;
57+
Console.WriteLine(i);
6058
// Output: 14
6159

6260
string story = "Start. ";
6361
story += "End.";
6462
Console.WriteLine(story);
6563
// Output: Start. End.
6664

67-
Action<int> printer = (int s) => Console.WriteLine(s);
68-
printer += (int s) => Console.WriteLine(2 * s);
69-
printer(3);
70-
// Output:
71-
// 3
72-
// 6
65+
Action printer = () => Console.Write("a");
66+
printer(); // output: a
67+
68+
Console.WriteLine();
69+
printer += () => Console.Write("b");
70+
printer(); // output: ab
7371
// </SnippetAddAndAssign>
72+
Console.WriteLine();
7473
}
7574
}
7675
}

0 commit comments

Comments
 (0)