Skip to content

Commit c5217ae

Browse files
pkulikovBillWagner
authored andcommitted
Added snippets for the subtraction operator article (#929)
1 parent bcc4c5e commit c5217ae

File tree

3 files changed

+86
-0
lines changed

3 files changed

+86
-0
lines changed
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
using System;
2+
3+
namespace operators
4+
{
5+
class Program
6+
{
7+
static void Main(string[] args)
8+
{
9+
Console.WriteLine("============== - operator examples =============");
10+
SubtractionOperator.Examples();
11+
Console.WriteLine();
12+
}
13+
}
14+
}
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
using System;
2+
3+
namespace operators
4+
{
5+
public static class SubtractionOperator
6+
{
7+
public static void Examples()
8+
{
9+
DelegateRemoval();
10+
SubtractAndAssign();
11+
}
12+
13+
private static void DelegateRemoval()
14+
{
15+
// <SnippetDelegateRemoval>
16+
Action a = () => Console.Write("a");
17+
Action b = () => Console.Write("b");
18+
19+
var abbaab = a + b + b + a + a + b;
20+
abbaab(); // output: abbaab
21+
Console.WriteLine();
22+
23+
var ab = a + b;
24+
var abba = abbaab - ab;
25+
abba(); // output: abba
26+
Console.WriteLine();
27+
28+
var aba = a + b + a;
29+
var first = abbaab - aba;
30+
first(); // output: abbaab
31+
Console.WriteLine();
32+
33+
var nihil = abbaab - abbaab;
34+
Console.WriteLine(nihil is null); // output: True
35+
// </SnippetDelegateRemoval>
36+
}
37+
38+
private static void SubtractAndAssign()
39+
{
40+
// <SnippetSubtractAndAssign>
41+
int i = 5;
42+
i -= 9;
43+
Console.WriteLine(i);
44+
// Output: -4
45+
46+
Action a = () => Console.Write("a");
47+
Action b = () => Console.Write("b");
48+
var printer = a + b + a;
49+
printer(); // output: aba
50+
51+
Console.WriteLine();
52+
printer -= a;
53+
printer(); // output: ab
54+
// </SnippetSubtractAndAssign>
55+
Console.WriteLine();
56+
}
57+
}
58+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>netcoreapp2.2</TargetFramework>
6+
</PropertyGroup>
7+
8+
<PropertyGroup>
9+
<LangVersion>7.3</LangVersion>
10+
<StartupObject>operators.Program</StartupObject>
11+
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
12+
</PropertyGroup>
13+
14+
</Project>

0 commit comments

Comments
 (0)