From 7136569f5d9a5d9f1ffaff98d4544c1dcd406df1 Mon Sep 17 00:00:00 2001 From: pkulikov Date: Mon, 27 May 2019 08:12:29 +0200 Subject: [PATCH] Added snippets for the subtraction operator article --- .../language-reference/operators/Program.cs | 14 +++++ .../operators/SubtractionOperator.cs | 58 +++++++++++++++++++ .../operators/operators.csproj | 14 +++++ 3 files changed, 86 insertions(+) create mode 100644 csharp/language-reference/operators/Program.cs create mode 100644 csharp/language-reference/operators/SubtractionOperator.cs create mode 100644 csharp/language-reference/operators/operators.csproj diff --git a/csharp/language-reference/operators/Program.cs b/csharp/language-reference/operators/Program.cs new file mode 100644 index 00000000000..6272c68cd48 --- /dev/null +++ b/csharp/language-reference/operators/Program.cs @@ -0,0 +1,14 @@ +using System; + +namespace operators +{ + class Program + { + static void Main(string[] args) + { + Console.WriteLine("============== - operator examples ============="); + SubtractionOperator.Examples(); + Console.WriteLine(); + } + } +} diff --git a/csharp/language-reference/operators/SubtractionOperator.cs b/csharp/language-reference/operators/SubtractionOperator.cs new file mode 100644 index 00000000000..3d0892f1a27 --- /dev/null +++ b/csharp/language-reference/operators/SubtractionOperator.cs @@ -0,0 +1,58 @@ +using System; + +namespace operators +{ + public static class SubtractionOperator + { + public static void Examples() + { + DelegateRemoval(); + SubtractAndAssign(); + } + + private static void DelegateRemoval() + { + // + Action a = () => Console.Write("a"); + Action b = () => Console.Write("b"); + + var abbaab = a + b + b + a + a + b; + abbaab(); // output: abbaab + Console.WriteLine(); + + var ab = a + b; + var abba = abbaab - ab; + abba(); // output: abba + Console.WriteLine(); + + var aba = a + b + a; + var first = abbaab - aba; + first(); // output: abbaab + Console.WriteLine(); + + var nihil = abbaab - abbaab; + Console.WriteLine(nihil is null); // output: True + // + } + + private static void SubtractAndAssign() + { + // + int i = 5; + i -= 9; + Console.WriteLine(i); + // Output: -4 + + Action a = () => Console.Write("a"); + Action b = () => Console.Write("b"); + var printer = a + b + a; + printer(); // output: aba + + Console.WriteLine(); + printer -= a; + printer(); // output: ab + // + Console.WriteLine(); + } + } +} \ No newline at end of file diff --git a/csharp/language-reference/operators/operators.csproj b/csharp/language-reference/operators/operators.csproj new file mode 100644 index 00000000000..05f268b5cc3 --- /dev/null +++ b/csharp/language-reference/operators/operators.csproj @@ -0,0 +1,14 @@ + + + + Exe + netcoreapp2.2 + + + + 7.3 + operators.Program + true + + +