From ec50ed2c9914e947f21a55ab3070b45c99850200 Mon Sep 17 00:00:00 2001 From: Petr Kulikov Date: Thu, 12 Mar 2020 15:44:33 +0100 Subject: [PATCH] Removed not used samples (#2056) --- .../builtin-types/BoolType.cs | 22 -- .../builtin-types/CharType.cs | 26 -- .../builtin-types/EnumType.cs | 80 ------ .../builtin-types/NullableValueTypes.cs | 221 --------------- .../builtin-types/Program.cs | 42 --- .../builtin-types/StructType.cs | 50 ---- .../builtin-types/UnmanagedTypes.cs | 37 --- .../builtin-types/ValueTypes.cs | 95 ------- .../builtin-types/VoidType.cs | 25 -- .../builtin-types/builtin-types.csproj | 11 - .../operators/AdditionOperator.cs | 75 ----- .../operators/ArithmeticOperators.cs | 263 ------------------ .../operators/AssignmentOperator.cs | 60 ---- .../operators/AwaitOperator.cs | 31 --- .../operators/BitwiseAndShiftOperators.cs | 205 -------------- .../operators/BooleanLogicalOperators.cs | 189 ------------- .../operators/ComparisonOperators.cs | 67 ----- .../operators/ConditionalOperator.cs | 67 ----- .../operators/DefaultOperator.cs | 63 ----- .../operators/DelegateOperator.cs | 46 --- .../operators/EqualityOperators.cs | 119 -------- .../operators/LambdaOperator.cs | 47 ---- .../operators/MemberAccessOperators.cs | 174 ------------ .../operators/NameOfOperator.cs | 36 --- .../operators/NewOperator.cs | 82 ------ .../operators/NullCoalescingOperator.cs | 73 ----- .../operators/NullForgivingOperator.cs | 70 ----- .../operators/OperatorOverloading.cs | 54 ---- .../operators/PointerOperators.cs | 170 ----------- .../language-reference/operators/Program.cs | 107 ------- .../operators/SizeOfOperator.cs | 32 --- .../operators/StackallocOperator.cs | 65 ----- .../operators/SubtractionOperator.cs | 95 ------- .../operators/TrueFalseOperators.cs | 60 ---- .../TypeTestingAndConversionOperators.cs | 136 --------- .../operators/UserDefinedConversions.cs | 34 --- .../operators/operators.csproj | 15 - 37 files changed, 3044 deletions(-) delete mode 100644 csharp/language-reference/builtin-types/BoolType.cs delete mode 100644 csharp/language-reference/builtin-types/CharType.cs delete mode 100644 csharp/language-reference/builtin-types/EnumType.cs delete mode 100644 csharp/language-reference/builtin-types/NullableValueTypes.cs delete mode 100644 csharp/language-reference/builtin-types/Program.cs delete mode 100644 csharp/language-reference/builtin-types/StructType.cs delete mode 100644 csharp/language-reference/builtin-types/UnmanagedTypes.cs delete mode 100644 csharp/language-reference/builtin-types/ValueTypes.cs delete mode 100644 csharp/language-reference/builtin-types/VoidType.cs delete mode 100644 csharp/language-reference/builtin-types/builtin-types.csproj delete mode 100644 csharp/language-reference/operators/AdditionOperator.cs delete mode 100644 csharp/language-reference/operators/ArithmeticOperators.cs delete mode 100644 csharp/language-reference/operators/AssignmentOperator.cs delete mode 100644 csharp/language-reference/operators/AwaitOperator.cs delete mode 100644 csharp/language-reference/operators/BitwiseAndShiftOperators.cs delete mode 100644 csharp/language-reference/operators/BooleanLogicalOperators.cs delete mode 100644 csharp/language-reference/operators/ComparisonOperators.cs delete mode 100644 csharp/language-reference/operators/ConditionalOperator.cs delete mode 100644 csharp/language-reference/operators/DefaultOperator.cs delete mode 100644 csharp/language-reference/operators/DelegateOperator.cs delete mode 100644 csharp/language-reference/operators/EqualityOperators.cs delete mode 100644 csharp/language-reference/operators/LambdaOperator.cs delete mode 100644 csharp/language-reference/operators/MemberAccessOperators.cs delete mode 100644 csharp/language-reference/operators/NameOfOperator.cs delete mode 100644 csharp/language-reference/operators/NewOperator.cs delete mode 100644 csharp/language-reference/operators/NullCoalescingOperator.cs delete mode 100644 csharp/language-reference/operators/NullForgivingOperator.cs delete mode 100644 csharp/language-reference/operators/OperatorOverloading.cs delete mode 100644 csharp/language-reference/operators/PointerOperators.cs delete mode 100644 csharp/language-reference/operators/Program.cs delete mode 100644 csharp/language-reference/operators/SizeOfOperator.cs delete mode 100644 csharp/language-reference/operators/StackallocOperator.cs delete mode 100644 csharp/language-reference/operators/SubtractionOperator.cs delete mode 100644 csharp/language-reference/operators/TrueFalseOperators.cs delete mode 100644 csharp/language-reference/operators/TypeTestingAndConversionOperators.cs delete mode 100644 csharp/language-reference/operators/UserDefinedConversions.cs delete mode 100644 csharp/language-reference/operators/operators.csproj diff --git a/csharp/language-reference/builtin-types/BoolType.cs b/csharp/language-reference/builtin-types/BoolType.cs deleted file mode 100644 index 734a7528a17..00000000000 --- a/csharp/language-reference/builtin-types/BoolType.cs +++ /dev/null @@ -1,22 +0,0 @@ -using System; - -namespace builtin_types -{ - public static class BoolType - { - public static void Examples() - { - Literals(); - } - - private static void Literals() - { - // - bool check = true; - Console.WriteLine(check ? "Checked" : "Not checked"); // output: Checked - - Console.WriteLine(false ? "Checked" : "Not checked"); // output: Not checked - // - } - } -} \ No newline at end of file diff --git a/csharp/language-reference/builtin-types/CharType.cs b/csharp/language-reference/builtin-types/CharType.cs deleted file mode 100644 index d6cbdcfb67b..00000000000 --- a/csharp/language-reference/builtin-types/CharType.cs +++ /dev/null @@ -1,26 +0,0 @@ -using System; - -namespace builtin_types -{ - public static class CharType - { - public static void Examples() - { - Literals(); - } - - private static void Literals() - { - // - var chars = new[] - { - 'j', - '\u006A', - '\x006A', - (char)106, - }; - Console.WriteLine(string.Join(" ", chars)); // output: j j j j - // - } - } -} \ No newline at end of file diff --git a/csharp/language-reference/builtin-types/EnumType.cs b/csharp/language-reference/builtin-types/EnumType.cs deleted file mode 100644 index c27bfb8975b..00000000000 --- a/csharp/language-reference/builtin-types/EnumType.cs +++ /dev/null @@ -1,80 +0,0 @@ -using System; - -namespace builtin_types -{ - public static class EnumType - { - public static void Examples() - { - FlagsEnumExample.Main(); - EnumConversionExample.Main(); - } - - // - [Flags] - public enum Days - { - None = 0b_0000_0000, // 0 - Monday = 0b_0000_0001, // 1 - Tuesday = 0b_0000_0010, // 2 - Wednesday = 0b_0000_0100, // 4 - Thursday = 0b_0000_1000, // 8 - Friday = 0b_0001_0000, // 16 - Saturday = 0b_0010_0000, // 32 - Sunday = 0b_0100_0000, // 64 - Weekend = Saturday | Sunday - } - - public class FlagsEnumExample - { - public static void Main() - { - Days meetingDays = Days.Monday | Days.Wednesday | Days.Friday; - Console.WriteLine(meetingDays); - // Output: - // Monday, Wednesday, Friday - - Days workingFromHomeDays = Days.Thursday | Days.Friday; - Console.WriteLine($"Join a meeting by phone on {meetingDays & workingFromHomeDays}"); - // Output: - // Join a meeting by phone on Friday - - bool isMeetingOnTuesday = (meetingDays & Days.Tuesday) == Days.Tuesday; - Console.WriteLine($"Is there a meeting on Tuesday: {isMeetingOnTuesday}"); - // Output: - // Is there a meeting on Tuesday: False - - var a = (Days)37; - Console.WriteLine(a); - // Output: - // Monday, Wednesday, Saturday - } - } - // - - // - public enum Season - { - Spring, - Summer, - Autumn, - Winter - } - - public class EnumConversionExample - { - public static void Main() - { - Season a = Season.Autumn; - Console.WriteLine($"Integral value of {a} is {(int)a}"); // output: Integral value of Autumn is 2 - - var b = (Season)1; - Console.WriteLine(b); // output: Summer - - var c = (Season)4; - Console.WriteLine(c); // output: 4 - } - } - // - } -} diff --git a/csharp/language-reference/builtin-types/NullableValueTypes.cs b/csharp/language-reference/builtin-types/NullableValueTypes.cs deleted file mode 100644 index 307a1f0e0a5..00000000000 --- a/csharp/language-reference/builtin-types/NullableValueTypes.cs +++ /dev/null @@ -1,221 +0,0 @@ -using System; - -namespace builtin_types -{ - public static class NullableValueTypes - { - public static void Examples() - { - DeclareAndAssign(); - ExamineValueOfNullableType(); - UseNullCoalescingOperator(); - Operators(); - ComparisonOperators(); - BoxingAndUnboxing(); - WhetherTypeIsNullable(); - GetTypeExample(); - IsOperatorExample(); - WhetherInstanceIsOfNullableType(); - } - - private static void DeclareAndAssign() - { - // - double? pi = 3.14; - char? letter = 'a'; - - int m2 = 10; - int? m = m2; - - bool? flag = null; - - // An array of a nullable type: - int?[] arr = new int?[10]; - // - } - - private static void ExamineValueOfNullableType() - { - // - int? a = 42; - if (a is int valueOfA) - { - Console.WriteLine($"a is {valueOfA}"); - } - else - { - Console.WriteLine("a does not have a value"); - } - // Output: - // a is 42 - // - - // - int? b = 10; - if (b.HasValue) - { - Console.WriteLine($"b is {b.Value}"); - } - else - { - Console.WriteLine("b does not have a value"); - } - // Output: - // b is 10 - // - - // - int? c = 7; - if (c != null) - { - Console.WriteLine($"c is {c.Value}"); - } - else - { - Console.WriteLine("c does not have a value"); - } - // Output: - // c is 7 - // - } - - private static void UseNullCoalescingOperator() - { - // - int? a = 28; - int b = a ?? -1; - Console.WriteLine($"b is {b}"); // output: b is 28 - - int? c = null; - int d = c ?? -1; - Console.WriteLine($"d is {d}"); // output: d is -1 - // - } - - private static void ExplicitCast() - { - // - int? n = null; - - //int m1 = n; // Doesn't compile - int n2 = (int)n; // Compiles, but throws an exception if n is null - // - } - - private static void Operators() - { - // - int? a = 10; - int? b = null; - int? c = 10; - - a++; // a is 11 - a = a * c; // a is 110 - a = a + b; // a is null - // - } - - private static void ComparisonOperators() - { - // - int? a = 10; - Console.WriteLine($"{a} >= null is {a >= null}"); - Console.WriteLine($"{a} < null is {a < null}"); - Console.WriteLine($"{a} == null is {a == null}"); - // Output: - // 10 >= null is False - // 10 < null is False - // 10 == null is False - - int? b = null; - int? c = null; - Console.WriteLine($"null >= null is {b >= c}"); - Console.WriteLine($"null == null is {b == c}"); - // Output: - // null >= null is False - // null == null is True - // - } - - private static void BoxingAndUnboxing() - { - // - int a = 41; - object aBoxed = a; - int? aNullable = (int?)aBoxed; - Console.WriteLine($"Value of aNullable: {aNullable}"); - - object aNullableBoxed = aNullable; - if (aNullableBoxed is int valueOfA) - { - Console.WriteLine($"aNullableBoxed is boxed int: {valueOfA}"); - } - // Output: - // Value of aNullable: 41 - // aNullableBoxed is boxed int: 41 - // - } - - private static void WhetherTypeIsNullable() - { - // - Console.WriteLine($"int? is {(IsNullable(typeof(int?)) ? "nullable" : "non nullable")} type"); - Console.WriteLine($"int is {(IsNullable(typeof(int)) ? "nullable" : "non-nullable")} type"); - - bool IsNullable(Type type) => Nullable.GetUnderlyingType(type) != null; - - // Output: - // int? is nullable type - // int is non-nullable type - // - } - - private static void GetTypeExample() - { - // - int? a = 17; - Type typeOfA = a.GetType(); - Console.WriteLine(typeOfA.FullName); - // Output: - // System.Int32 - // - } - - private static void IsOperatorExample() - { - // - int? a = 14; - if (a is int) - { - Console.WriteLine("int? instance is compatible with int"); - } - - int b = 17; - if (b is int?) - { - Console.WriteLine("int instance is compatible with int?"); - } - // Output: - // int? instance is compatible with int - // int instance is compatible with int? - // - } - - private static void WhetherInstanceIsOfNullableType() - { - // - int? a = 14; - Console.WriteLine(IsOfNullableType(a)); // output: True - - int b = 17; - Console.WriteLine(IsOfNullableType(b)); // output: False - - bool IsOfNullableType(T o) - { - var type = typeof(T); - return Nullable.GetUnderlyingType(type) != null; - } - // - } - } -} \ No newline at end of file diff --git a/csharp/language-reference/builtin-types/Program.cs b/csharp/language-reference/builtin-types/Program.cs deleted file mode 100644 index fa3b1ff0b59..00000000000 --- a/csharp/language-reference/builtin-types/Program.cs +++ /dev/null @@ -1,42 +0,0 @@ -using System; - -namespace builtin_types -{ - class Program - { - static void Main(string[] args) - { - Console.WriteLine("======== Unmanaged types ========="); - UnmanagedTypes.Main(); - Console.WriteLine(); - - Console.WriteLine("====== Nullable value types ======"); - NullableValueTypes.Examples(); - Console.WriteLine(); - - Console.WriteLine("============ char type ==========="); - CharType.Examples(); - Console.WriteLine(); - - Console.WriteLine("============ bool type ==========="); - BoolType.Examples(); - Console.WriteLine(); - - Console.WriteLine("========= Enumeration types ======"); - EnumType.Examples(); - Console.WriteLine(); - - Console.WriteLine("============ Value types ========="); - ValueTypes.Examples(); - Console.WriteLine(); - - Console.WriteLine("============= void type =========="); - VoidType.Examples(); - Console.WriteLine(); - - Console.WriteLine("========== Structure types ======="); - StructType.Examples(); - Console.WriteLine(); - } - } -} diff --git a/csharp/language-reference/builtin-types/StructType.cs b/csharp/language-reference/builtin-types/StructType.cs deleted file mode 100644 index 8d4fce12d73..00000000000 --- a/csharp/language-reference/builtin-types/StructType.cs +++ /dev/null @@ -1,50 +0,0 @@ -using System; - -namespace builtin_types -{ - public static class StructType - { - public static void Examples() - { - without_new.StructWithoutNew.Main(); - } - - // - public struct Coords - { - public Coords(double x, double y) - { - X = x; - Y = y; - } - - public double X { get; } - public double Y { get; } - - public override string ToString() => $"({X}, {Y})"; - } - // - } - - namespace without_new - { - // - public static class StructWithoutNew - { - public struct Coords - { - public double x; - public double y; - } - - public static void Main() - { - Coords p; - p.x = 3; - p.y = 4; - Console.WriteLine($"({p.x}, {p.y})"); // output: (3, 4) - } - } - // - } -} \ No newline at end of file diff --git a/csharp/language-reference/builtin-types/UnmanagedTypes.cs b/csharp/language-reference/builtin-types/UnmanagedTypes.cs deleted file mode 100644 index ef542656b24..00000000000 --- a/csharp/language-reference/builtin-types/UnmanagedTypes.cs +++ /dev/null @@ -1,37 +0,0 @@ -// -using System; - -public struct Coords -{ - public T X; - public T Y; -} - -public class UnmanagedTypes -{ - public static void Main() - { - DisplaySize>(); - DisplaySize>(); - } - - private unsafe static void DisplaySize() where T : unmanaged - { - Console.WriteLine($"{typeof(T)} is unmanaged and its size is {sizeof(T)} bytes"); - } -} -// Output: -// Coords`1[System.Int32] is unmanaged and its size is 8 bytes -// Coords`1[System.Double] is unmanaged and its size is 16 bytes -// - -namespace always_unmanaged -{ - // - public struct Coords where T : unmanaged - { - public T X; - public T Y; - } - // -} \ No newline at end of file diff --git a/csharp/language-reference/builtin-types/ValueTypes.cs b/csharp/language-reference/builtin-types/ValueTypes.cs deleted file mode 100644 index a3808710542..00000000000 --- a/csharp/language-reference/builtin-types/ValueTypes.cs +++ /dev/null @@ -1,95 +0,0 @@ -namespace builtin_types -{ - public static class ValueTypes - { - public static void Examples() - { - example_value_copy.Program.Main(); - example_shallow_copy.Program.Main(); - } - } - - namespace example_value_copy - { - // - using System; - - public struct MutablePoint - { - public int X; - public int Y; - - public MutablePoint(int x, int y) => (X, Y) = (x, y); - - public override string ToString() => $"({X}, {Y})"; - } - - public class Program - { - public static void Main() - { - var p1 = new MutablePoint(1, 2); - var p2 = p1; - p2.Y = 200; - Console.WriteLine($"{nameof(p1)} after {nameof(p2)} is modified: {p1}"); - Console.WriteLine($"{nameof(p2)}: {p2}"); - - MutateAndDisplay(p2); - Console.WriteLine($"{nameof(p2)} after passing to a method: {p2}"); - } - - private static void MutateAndDisplay(MutablePoint p) - { - p.X = 100; - Console.WriteLine($"Point mutated in a method: {p}"); - } - } - // Expected output: - // p1 after p2 is modified: (1, 2) - // p2: (1, 200) - // Point mutated in a method: (100, 200) - // p2 after passing to a method: (1, 200) - // - } - - namespace example_shallow_copy - { - // - using System; - using System.Collections.Generic; - - public struct TaggedInteger - { - public int Number; - private List tags; - - public TaggedInteger(int n) - { - Number = n; - tags = new List(); - } - - public void AddTag(string tag) => tags.Add(tag); - - public override string ToString() => $"{Number} [{string.Join(", ", tags)}]"; - } - - public class Program - { - public static void Main() - { - var n1 = new TaggedInteger(0); - n1.AddTag("A"); - Console.WriteLine(n1); // output: 0 [A] - - var n2 = n1; - n2.Number = 7; - n2.AddTag("B"); - - Console.WriteLine(n1); // output: 0 [A, B] - Console.WriteLine(n2); // output: 7 [A, B] - } - } - // - } -} \ No newline at end of file diff --git a/csharp/language-reference/builtin-types/VoidType.cs b/csharp/language-reference/builtin-types/VoidType.cs deleted file mode 100644 index c4f4070972e..00000000000 --- a/csharp/language-reference/builtin-types/VoidType.cs +++ /dev/null @@ -1,25 +0,0 @@ -using System; -using System.Collections.Generic; - -namespace builtin_types -{ - public static class VoidType - { - public static void Examples() - { - Display(new[] { 1, 2, 9 }); - } - - // - public static void Display(IEnumerable numbers) - { - if (numbers is null) - { - return; - } - - Console.WriteLine(string.Join(" ", numbers)); - } - // - } -} \ No newline at end of file diff --git a/csharp/language-reference/builtin-types/builtin-types.csproj b/csharp/language-reference/builtin-types/builtin-types.csproj deleted file mode 100644 index 91332bd63ab..00000000000 --- a/csharp/language-reference/builtin-types/builtin-types.csproj +++ /dev/null @@ -1,11 +0,0 @@ - - - - Exe - netcoreapp3.1 - 8.0 - builtin_types.Program - true - - - diff --git a/csharp/language-reference/operators/AdditionOperator.cs b/csharp/language-reference/operators/AdditionOperator.cs deleted file mode 100644 index 3783a8af458..00000000000 --- a/csharp/language-reference/operators/AdditionOperator.cs +++ /dev/null @@ -1,75 +0,0 @@ -using System; - -namespace operators -{ - public static class AdditionOperator - { - public static void Examples() - { - NumericAddition(); - StringConcatenation(); - AddDelegates(); - AddAndAssign(); - } - - private static void NumericAddition() - { - // - Console.WriteLine(5 + 4); // output: 9 - Console.WriteLine(5 + 4.3); // output: 9.3 - Console.WriteLine(5.1m + 4.2m); // output: 9.3 - // - } - - private static void StringConcatenation() - { - // - Console.WriteLine("Forgot" + "white space"); - Console.WriteLine("Probably the oldest constant: " + Math.PI); - // Output: - // Forgotwhite space - // Probably the oldest constant: 3.14159265358979 - // - - // - Console.WriteLine($"Probably the oldest constant: {Math.PI:F2}"); - // Output: - // Probably the oldest constant: 3.14 - // - } - - private static void AddDelegates() - { - // - Action a = () => Console.Write("a"); - Action b = () => Console.Write("b"); - Action ab = a + b; - ab(); // output: ab - // - Console.WriteLine(); - } - - private static void AddAndAssign() - { - // - int i = 5; - i += 9; - Console.WriteLine(i); - // Output: 14 - - string story = "Start. "; - story += "End."; - Console.WriteLine(story); - // Output: Start. End. - - Action printer = () => Console.Write("a"); - printer(); // output: a - - Console.WriteLine(); - printer += () => Console.Write("b"); - printer(); // output: ab - // - Console.WriteLine(); - } - } -} diff --git a/csharp/language-reference/operators/ArithmeticOperators.cs b/csharp/language-reference/operators/ArithmeticOperators.cs deleted file mode 100644 index 4842a262b18..00000000000 --- a/csharp/language-reference/operators/ArithmeticOperators.cs +++ /dev/null @@ -1,263 +0,0 @@ -using System; - -namespace operators -{ - public static class ArithmeticOperators - { - public static void Examples() - { - Console.WriteLine("==== ++ and -- operators"); - Increment(); - Decrement(); - - Console.WriteLine("==== Unary + and - operators"); - UnaryPlusAndMinus(); - - Console.WriteLine("==== *, /, %, +, and - operators"); - Multiplication(); - IntegerDivision(); - IntegerAsFloatingPointDivision(); - FloatingPointDivision(); - IntegerRemainder(); - FloatingPointRemainder(); - Addition(); - Subtraction(); - - Console.WriteLine("==== Precedence and associativity examples"); - PrecedenceAndAssociativity(); - - Console.WriteLine("==== Compound assignment"); - CompoundAssignment(); - CompoundAssignmentWithCast(); - - Console.WriteLine("==== Special cases"); - CheckedUnchecked(); - FloatingPointOverflow(); - RoundOffErrors(); - } - - private static void Increment() - { - // - double a = 1.5; - Console.WriteLine(a); // output: 1.5 - Console.WriteLine(++a); // output: 2.5 - Console.WriteLine(a); // output: 2.5 - // - - // - int i = 3; - Console.WriteLine(i); // output: 3 - Console.WriteLine(i++); // output: 3 - Console.WriteLine(i); // output: 4 - // - } - - private static void Decrement() - { - // - double a = 1.5; - Console.WriteLine(a); // output: 1.5 - Console.WriteLine(--a); // output: 0.5 - Console.WriteLine(a); // output: 0.5 - // - - // - int i = 3; - Console.WriteLine(i); // output: 3 - Console.WriteLine(i--); // output: 3 - Console.WriteLine(i); // output: 2 - // - } - - private static void UnaryPlusAndMinus() - { - // - Console.WriteLine(+4); // output: 4 - - Console.WriteLine(-4); // output: -4 - Console.WriteLine(-(-4)); // output: 4 - - uint a = 5; - var b = -a; - Console.WriteLine(b); // output: -5 - Console.WriteLine(b.GetType()); // output: System.Int64 - - Console.WriteLine(-double.NaN); // output: NaN - // - } - - private static void Multiplication() - { - // - Console.WriteLine(5 * 2); // output: 10 - Console.WriteLine(0.5 * 2.5); // output: 1.25 - Console.WriteLine(0.1m * 23.4m); // output: 2.34 - // - } - - private static void IntegerDivision() - { - // - Console.WriteLine(13 / 5); // output: 2 - Console.WriteLine(-13 / 5); // output: -2 - Console.WriteLine(13 / -5); // output: -2 - Console.WriteLine(-13 / -5); // output: 2 - // - } - - private static void IntegerAsFloatingPointDivision() - { - // - Console.WriteLine(13 / 5.0); // output: 2.6 - - int a = 13; - int b = 5; - Console.WriteLine((double)a / b); // output: 2.6 - // - } - - private static void FloatingPointDivision() - { - // - Console.WriteLine(16.8f / 4.1f); // output: 4.097561 - Console.WriteLine(16.8d / 4.1d); // output: 4.09756097560976 - Console.WriteLine(16.8m / 4.1m); // output: 4.0975609756097560975609756098 - // - } - - private static void IntegerRemainder() - { - // - Console.WriteLine(5 % 4); // output: 1 - Console.WriteLine(5 % -4); // output: 1 - Console.WriteLine(-5 % 4); // output: -1 - Console.WriteLine(-5 % -4); // output: -1 - // - } - - private static void FloatingPointRemainder() - { - // - Console.WriteLine(-5.2f % 2.0f); // output: -1.2 - Console.WriteLine(5.9 % 3.1); // output: 2.8 - Console.WriteLine(5.9m % 3.1m); // output: 2.8 - // - } - - private static void Addition() - { - // - Console.WriteLine(5 + 4); // output: 9 - Console.WriteLine(5 + 4.3); // output: 9.3 - Console.WriteLine(5.1m + 4.2m); // output: 9.3 - // - } - - private static void Subtraction() - { - // - Console.WriteLine(47 - 3); // output: 44 - Console.WriteLine(5 - 4.3); // output: 0.7 - Console.WriteLine(7.5m - 2.3m); // output: 5.2 - // - } - - private static void PrecedenceAndAssociativity() - { - // - Console.WriteLine(2 + 2 * 2); // output: 6 - Console.WriteLine((2 + 2) * 2); // output: 8 - - Console.WriteLine(9 / 5 / 2); // output: 0 - Console.WriteLine(9 / (5 / 2)); // output: 4 - // - } - - private static void CompoundAssignment() - { - // - int a = 5; - a += 9; - Console.WriteLine(a); // output: 14 - - a -= 4; - Console.WriteLine(a); // output: 10 - - a *= 2; - Console.WriteLine(a); // output: 20 - - a /= 4; - Console.WriteLine(a); // output: 5 - - a %= 3; - Console.WriteLine(a); // output: 2 - // - } - - private static void CompoundAssignmentWithCast() - { - // - byte a = 200; - byte b = 100; - - var c = a + b; - Console.WriteLine(c.GetType()); // output: System.Int32 - Console.WriteLine(c); // output: 300 - - a += b; - Console.WriteLine(a); // output: 44 - // - } - - private static void CheckedUnchecked() - { - // - int a = int.MaxValue; - int b = 3; - - Console.WriteLine(unchecked(a + b)); // output: -2147483646 - try - { - int d = checked(a + b); - } - catch(OverflowException) - { - Console.WriteLine($"Overflow occurred when adding {a} to {b}."); - } - // - } - - private static void FloatingPointOverflow() - { - // - double a = 1.0 / 0.0; - Console.WriteLine(a); // output: Infinity - Console.WriteLine(double.IsInfinity(a)); // output: True - - Console.WriteLine(double.MaxValue + double.MaxValue); // output: Infinity - - double b = 0.0 / 0.0; - Console.WriteLine(b); // output: NaN - Console.WriteLine(double.IsNaN(b)); // output: True - // - } - - private static void RoundOffErrors() - { - // - Console.WriteLine(.41f % .2f); // output: 0.00999999 - - double a = 0.1; - double b = 3 * a; - Console.WriteLine(b == 0.3); // output: False - Console.WriteLine(b - 0.3); // output: 5.55111512312578E-17 - - decimal c = 1 / 3.0m; - decimal d = 3 * c; - Console.WriteLine(d == 1.0m); // output: False - Console.WriteLine(d); // output: 0.9999999999999999999999999999 - // - } - } -} diff --git a/csharp/language-reference/operators/AssignmentOperator.cs b/csharp/language-reference/operators/AssignmentOperator.cs deleted file mode 100644 index a93ad26602a..00000000000 --- a/csharp/language-reference/operators/AssignmentOperator.cs +++ /dev/null @@ -1,60 +0,0 @@ -using System; -using System.Collections.Generic; - -namespace operators -{ - public static class AssignmentOperator - { - public static void Examples() - { - Simple(); - RefAssignment(); - } - - private static void Simple() - { - // - var numbers = new List() { 1.0, 2.0, 3.0 }; - - Console.WriteLine(numbers.Capacity); - numbers.Capacity = 100; - Console.WriteLine(numbers.Capacity); - // Output: - // 4 - // 100 - - int newFirstElement; - double originalFirstElement = numbers[0]; - newFirstElement = 5; - numbers[0] = newFirstElement; - Console.WriteLine(originalFirstElement); - Console.WriteLine(numbers[0]); - // Output: - // 1 - // 5 - // - } - - private static void RefAssignment() - { - // - void Display(double[] s) => Console.WriteLine(string.Join(" ", s)); - - double[] arr = { 0.0, 0.0, 0.0 }; - Display(arr); - - ref double arrayElement = ref arr[0]; - arrayElement = 3.0; - Display(arr); - - arrayElement = ref arr[arr.Length - 1]; - arrayElement = 5.0; - Display(arr); - // Output: - // 0 0 0 - // 3 0 0 - // 3 0 5 - // - } - } -} \ No newline at end of file diff --git a/csharp/language-reference/operators/AwaitOperator.cs b/csharp/language-reference/operators/AwaitOperator.cs deleted file mode 100644 index 31031ed9cd7..00000000000 --- a/csharp/language-reference/operators/AwaitOperator.cs +++ /dev/null @@ -1,31 +0,0 @@ -using System; -using System.Net.Http; -using System.Threading.Tasks; - -public class AwaitOperator -{ - public static async Task Main() - { - Task downloading = DownloadDocsMainPageAsync(); - Console.WriteLine($"{nameof(Main)}: Launched downloading."); - - int bytesLoaded = await downloading; - Console.WriteLine($"{nameof(Main)}: Downloaded {bytesLoaded} bytes."); - } - - private static async Task DownloadDocsMainPageAsync() - { - Console.WriteLine($"{nameof(DownloadDocsMainPageAsync)}: About to start downloading."); - - var client = new HttpClient(); - byte[] content = await client.GetByteArrayAsync("https://docs.microsoft.com/en-us/"); - - Console.WriteLine($"{nameof(DownloadDocsMainPageAsync)}: Finished downloading."); - return content.Length; - } -} -// Output similar to: -// DownloadDocsMainPageAsync: About to start downloading. -// Main: Launched downloading. -// DownloadDocsMainPageAsync: Finished downloading. -// Main: Downloaded 27700 bytes. diff --git a/csharp/language-reference/operators/BitwiseAndShiftOperators.cs b/csharp/language-reference/operators/BitwiseAndShiftOperators.cs deleted file mode 100644 index ea04231d450..00000000000 --- a/csharp/language-reference/operators/BitwiseAndShiftOperators.cs +++ /dev/null @@ -1,205 +0,0 @@ -using System; - -namespace operators -{ - public static class BitwiseAndShiftOperators - { - public static void Examples() - { - Console.WriteLine("==== ~, &, ^, and | operators"); - BitwiseComplement(); - BitwiseAnd(); - BitwiseXor(); - BitwiseOr(); - - Console.WriteLine("==== << and >> operators"); - LeftShift(); - RightShift(); - ShiftCount(); - - Console.WriteLine("==== Additional examples"); - CompoundAssignment(); - CompoundAssignmentWithCast(); - Precedence(); - } - - private static void BitwiseComplement() - { - // - uint a = 0b_0000_1111_0000_1111_0000_1111_0000_1100; - uint b = ~a; - Console.WriteLine(Convert.ToString(b, toBase: 2)); - // Output: - // 11110000111100001111000011110011 - // - } - - private static void BitwiseAnd() - { - // - uint a = 0b_1111_1000; - uint b = 0b_1001_1101; - uint c = a & b; - Console.WriteLine(Convert.ToString(c, toBase: 2)); - // Output: - // 10011000 - // - } - - private static void BitwiseXor() - { - // - uint a = 0b_1111_1000; - uint b = 0b_0001_1100; - uint c = a ^ b; - Console.WriteLine(Convert.ToString(c, toBase: 2)); - // Output: - // 11100100 - // - } - - private static void BitwiseOr() - { - // - uint a = 0b_1010_0000; - uint b = 0b_1001_0001; - uint c = a | b; - Console.WriteLine(Convert.ToString(c, toBase: 2)); - // Output: - // 10110001 - // - } - - private static void LeftShift() - { - // - uint x = 0b_1100_1001_0000_0000_0000_0000_0001_0001; - Console.WriteLine($"Before: {Convert.ToString(x, toBase: 2)}"); - - uint y = x << 4; - Console.WriteLine($"After: {Convert.ToString(y, toBase: 2)}"); - // Output: - // Before: 11001001000000000000000000010001 - // After: 10010000000000000000000100010000 - // - - // - byte a = 0b_1111_0001; - - var b = a << 8; - Console.WriteLine(b.GetType()); - Console.WriteLine($"Shifted byte: {Convert.ToString(b, toBase: 2)}"); - // Output: - // System.Int32 - // Shifted byte: 1111000100000000 - // - } - - private static void RightShift() - { - // - uint x = 0b_1001; - Console.WriteLine($"Before: {Convert.ToString(x, toBase: 2), 4}"); - - uint y = x >> 2; - Console.WriteLine($"After: {Convert.ToString(y, toBase: 2), 4}"); - // Output: - // Before: 1001 - // After: 10 - // - - // - int a = int.MinValue; - Console.WriteLine($"Before: {Convert.ToString(a, toBase: 2)}"); - - int b = a >> 3; - Console.WriteLine($"After: {Convert.ToString(b, toBase: 2)}"); - // Output: - // Before: 10000000000000000000000000000000 - // After: 11110000000000000000000000000000 - // - - // - uint c = 0b_1000_0000_0000_0000_0000_0000_0000_0000; - Console.WriteLine($"Before: {Convert.ToString(c, toBase: 2), 32}"); - - uint d = c >> 3; - Console.WriteLine($"After: {Convert.ToString(d, toBase: 2), 32}"); - // Output: - // Before: 10000000000000000000000000000000 - // After: 10000000000000000000000000000 - // - } - - private static void ShiftCount() - { - // - int count1 = 0b_0000_0001; - int count2 = 0b_1110_0001; - - int a = 0b_0001; - Console.WriteLine($"{a} << {count1} is {a << count1}; {a} << {count2} is {a << count2}"); - // Output: - // 1 << 1 is 2; 1 << 225 is 2 - - int b = 0b_0100; - Console.WriteLine($"{b} >> {count1} is {b >> count1}; {b} >> {count2} is {b >> count2}"); - // Output: - // 4 >> 1 is 2; 4 >> 225 is 2 - // - } - - private static void CompoundAssignment() - { - // - uint a = 0b_1111_1000; - a &= 0b_1001_1101; - Display(a); // output: 10011000 - - a |= 0b_0011_0001; - Display(a); // output: 10111001 - - a ^= 0b_1000_0000; - Display(a); // output: 111001 - - a <<= 2; - Display(a); // output: 11100100 - - a >>= 4; - Display(a); // output: 1110 - - void Display(uint x) => Console.WriteLine($"{Convert.ToString(x, toBase: 2), 8}"); - // - } - - private static void CompoundAssignmentWithCast() - { - // - byte x = 0b_1111_0001; - - int b = x << 8; - Console.WriteLine($"{Convert.ToString(b, toBase: 2)}"); // output: 1111000100000000 - - x <<= 8; - Console.WriteLine(x); // output: 0 - // - } - - private static void Precedence() - { - // - uint a = 0b_1101; - uint b = 0b_1001; - uint c = 0b_1010; - - uint d1 = a | b & c; - Display(d1); // output: 1101 - - uint d2 = (a | b) & c; - Display(d2); // output: 1000 - - void Display(uint x) => Console.WriteLine($"{Convert.ToString(x, toBase: 2), 4}"); - // - } - } -} \ No newline at end of file diff --git a/csharp/language-reference/operators/BooleanLogicalOperators.cs b/csharp/language-reference/operators/BooleanLogicalOperators.cs deleted file mode 100644 index 84228a61df7..00000000000 --- a/csharp/language-reference/operators/BooleanLogicalOperators.cs +++ /dev/null @@ -1,189 +0,0 @@ -using System; - -namespace operators -{ - public static class BooleanLogicalOperators - { - public static void Examples() - { - Console.WriteLine("==== !, &, |, ^ operators"); - Negation(); - And(); - Or(); - Xor(); - WithNullableBoolean(); - - Console.WriteLine("==== && and || operators"); - ConditionalAnd(); - ConditionalOr(); - - Console.WriteLine("==== Compound assignment and precedence"); - CompoundAssignment(); - Precedence(); - } - - private static void Negation() - { - // - bool passed = false; - Console.WriteLine(!passed); // output: True - Console.WriteLine(!true); // output: False - // - } - - private static void And() - { - // - bool SecondOperand() - { - Console.WriteLine("Second operand is evaluated."); - return true; - } - - bool a = false & SecondOperand(); - Console.WriteLine(a); - // Output: - // Second operand is evaluated. - // False - - bool b = true & SecondOperand(); - Console.WriteLine(b); - // Output: - // Second operand is evaluated. - // True - // - } - - private static void Or() - { - // - bool SecondOperand() - { - Console.WriteLine("Second operand is evaluated."); - return true; - } - - bool a = true | SecondOperand(); - Console.WriteLine(a); - // Output: - // Second operand is evaluated. - // True - - bool b = false | SecondOperand(); - Console.WriteLine(b); - // Output: - // Second operand is evaluated. - // True - // - } - - private static void Xor() - { - // - Console.WriteLine(true ^ true); // output: False - Console.WriteLine(true ^ false); // output: True - Console.WriteLine(false ^ true); // output: True - Console.WriteLine(false ^ false); // output: False - // - } - - private static void ConditionalAnd() - { - // - bool SecondOperand() - { - Console.WriteLine("Second operand is evaluated."); - return true; - } - - bool a = false && SecondOperand(); - Console.WriteLine(a); - // Output: - // False - - bool b = true && SecondOperand(); - Console.WriteLine(b); - // Output: - // Second operand is evaluated. - // True - // - } - - private static void ConditionalOr() - { - // - bool SecondOperand() - { - Console.WriteLine("Second operand is evaluated."); - return true; - } - - bool a = true || SecondOperand(); - Console.WriteLine(a); - // Output: - // True - - bool b = false || SecondOperand(); - Console.WriteLine(b); - // Output: - // Second operand is evaluated. - // True - // - } - - private static void WithNullableBoolean() - { - // - bool? test = null; - Display(!test); // output: null - Display(test ^ false); // output: null - Display(test ^ null); // output: null - Display(true ^ null); // output: null - - void Display(bool? b) => Console.WriteLine(b is null ? "null" : b.Value.ToString()); - // - } - - private static void CompoundAssignment() - { - // - bool test = true; - test &= false; - Console.WriteLine(test); // output: False - - test |= true; - Console.WriteLine(test); // output: True - - test ^= false; - Console.WriteLine(test); // output: True - // - } - - private static void Precedence() - { - // - Console.WriteLine(true | true & false); // output: True - Console.WriteLine((true | true) & false); // output: False - - bool Operand(string name, bool value) - { - Console.WriteLine($"Operand {name} is evaluated."); - return value; - } - - var byDefaultPrecedence = Operand("A", true) || Operand("B", true) && Operand("C", false); - Console.WriteLine(byDefaultPrecedence); - // Output: - // Operand A is evaluated. - // True - - var changedOrder = (Operand("A", true) || Operand("B", true)) && Operand("C", false); - Console.WriteLine(changedOrder); - // Output: - // Operand A is evaluated. - // Operand C is evaluated. - // False - // - } - } -} \ No newline at end of file diff --git a/csharp/language-reference/operators/ComparisonOperators.cs b/csharp/language-reference/operators/ComparisonOperators.cs deleted file mode 100644 index 7571b9b36fa..00000000000 --- a/csharp/language-reference/operators/ComparisonOperators.cs +++ /dev/null @@ -1,67 +0,0 @@ -using System; - -namespace operators -{ - public static class ComparisonOperators - { - public static void Examples() - { - Console.WriteLine("--- >"); - Greater(); - Console.WriteLine("--- <"); - Less(); - Console.WriteLine("--- >="); - GreaterOrEqual(); - Console.WriteLine("--- <="); - LessOrEqual(); - } - - private static void Greater() - { - // - Console.WriteLine(7.0 > 5.1); // output: True - Console.WriteLine(5.1 > 5.1); // output: False - Console.WriteLine(0.0 > 5.1); // output: False - - Console.WriteLine(double.NaN > 5.1); // output: False - Console.WriteLine(double.NaN <= 5.1); // output: False - // - } - - private static void Less() - { - // - Console.WriteLine(7.0 < 5.1); // output: False - Console.WriteLine(5.1 < 5.1); // output: False - Console.WriteLine(0.0 < 5.1); // output: True - - Console.WriteLine(double.NaN < 5.1); // output: False - Console.WriteLine(double.NaN >= 5.1); // output: False - // - } - - private static void GreaterOrEqual() - { - // - Console.WriteLine(7.0 >= 5.1); // output: True - Console.WriteLine(5.1 >= 5.1); // output: True - Console.WriteLine(0.0 >= 5.1); // output: False - - Console.WriteLine(double.NaN < 5.1); // output: False - Console.WriteLine(double.NaN >= 5.1); // output: False - // - } - - private static void LessOrEqual() - { - // - Console.WriteLine(7.0 <= 5.1); // output: False - Console.WriteLine(5.1 <= 5.1); // output: True - Console.WriteLine(0.0 <= 5.1); // output: True - - Console.WriteLine(double.NaN > 5.1); // output: False - Console.WriteLine(double.NaN <= 5.1); // output: False - // - } - } -} \ No newline at end of file diff --git a/csharp/language-reference/operators/ConditionalOperator.cs b/csharp/language-reference/operators/ConditionalOperator.cs deleted file mode 100644 index 3562e80704b..00000000000 --- a/csharp/language-reference/operators/ConditionalOperator.cs +++ /dev/null @@ -1,67 +0,0 @@ -using System; - -namespace operators -{ - public class ConditionalOperator - { - public static void Examples() - { - ConditionalRefExpressions(); - ConditionalValueExpressions(); - ComparisonWithIf(); - } - - private static void ConditionalRefExpressions() - { - // - var smallArray = new int[] { 1, 2, 3, 4, 5 }; - var largeArray = new int[] { 10, 20, 30, 40, 50 }; - - int index = 7; - ref int refValue = ref ((index < 5) ? ref smallArray[index] : ref largeArray[index - 5]); - refValue = 0; - - index = 2; - ((index < 5) ? ref smallArray[index] : ref largeArray[index - 5]) = 100; - - Console.WriteLine(string.Join(" ", smallArray)); - Console.WriteLine(string.Join(" ", largeArray)); - // Output: - // 1 2 100 4 5 - // 10 20 0 40 50 - // - } - - private static void ConditionalValueExpressions() - { - // - double sinc(double x) => x != 0.0 ? Math.Sin(x) / x : 1; - - Console.WriteLine(sinc(0.1)); - Console.WriteLine(sinc(0.0)); - // Output: - // 0.998334166468282 - // 1 - // - } - - private static void ComparisonWithIf() - { - // - int input = new Random().Next(-5, 5); - - string classify; - if (input >= 0) - { - classify = "nonnegative"; - } - else - { - classify = "negative"; - } - - classify = (input >= 0) ? "nonnegative" : "negative"; - // - } - } -} diff --git a/csharp/language-reference/operators/DefaultOperator.cs b/csharp/language-reference/operators/DefaultOperator.cs deleted file mode 100644 index 210e4ba93d8..00000000000 --- a/csharp/language-reference/operators/DefaultOperator.cs +++ /dev/null @@ -1,63 +0,0 @@ -using System; - -namespace operators -{ - public static class DefaultOperator - { - public static void Examples() - { - WithOperand(); - DefaultLiteral(); - } - - private static void WithOperand() - { - // - Console.WriteLine(default(int)); // output: 0 - Console.WriteLine(default(object) is null); // output: True - - void DisplayDefaultOf() - { - var val = default(T); - Console.WriteLine($"Default value of {typeof(T)} is {(val == null ? "null" : val.ToString())}."); - } - - DisplayDefaultOf(); - DisplayDefaultOf(); - DisplayDefaultOf>(); - // Output: - // Default value of System.Nullable`1[System.Int32] is null. - // Default value of System.Numerics.Complex is (0, 0). - // Default value of System.Collections.Generic.List`1[System.Int32] is null. - // - } - - private static void DefaultLiteral() - { - // - T[] InitializeArray(int length, T initialValue = default) - { - if (length < 0) - { - return default; - } - - var array = new T[length]; - for (var i = 0; i < length; i++) - { - array[i] = initialValue; - } - return array; - } - - void Display(T[] values) => Console.WriteLine($"[ {string.Join(", ", values)} ]"); - - Display(InitializeArray(3)); // output: [ 0, 0, 0 ] - Display(InitializeArray(4, default)); // output: [ False, False, False, False ] - - System.Numerics.Complex fillValue = default; - Display(InitializeArray(3, fillValue)); // output: [ (0, 0), (0, 0), (0, 0) ] - // - } - } -} \ No newline at end of file diff --git a/csharp/language-reference/operators/DelegateOperator.cs b/csharp/language-reference/operators/DelegateOperator.cs deleted file mode 100644 index 429acd6950c..00000000000 --- a/csharp/language-reference/operators/DelegateOperator.cs +++ /dev/null @@ -1,46 +0,0 @@ -using System; -using System.Collections.Generic; - -namespace operators -{ - public static class DelegateOperator - { - public static void Examples() - { - AnonymousMethod(); - Lambda(); - WithoutParameterList(); - } - - private static void AnonymousMethod() - { - // - Func sum = delegate (int a, int b) { return a + b; }; - Console.WriteLine(sum(3, 4)); // output: 7 - // - } - - private static void Lambda() - { - // - Func sum = (a, b) => a + b; - Console.WriteLine(sum(3, 4)); // output: 7 - // - } - - private static void WithoutParameterList() - { - // - Action greet = delegate { Console.WriteLine("Hello!"); }; - greet(); - - Action introduce = delegate { Console.WriteLine("This is world!"); }; - introduce(42, 2.7); - - // Output: - // Hello! - // This is world! - // - } - } -} \ No newline at end of file diff --git a/csharp/language-reference/operators/EqualityOperators.cs b/csharp/language-reference/operators/EqualityOperators.cs deleted file mode 100644 index 71df83a774f..00000000000 --- a/csharp/language-reference/operators/EqualityOperators.cs +++ /dev/null @@ -1,119 +0,0 @@ -using System; - -namespace operators -{ - public static class EqualityOperators - { - public static void Examples() - { - Console.WriteLine("Value types:"); - ValueTypesEquality(); - - Console.WriteLine("Strings:"); - StringEquality(); - - Console.WriteLine("Reference types:"); - ReferenceTypesEquality.Main(); - - Console.WriteLine("Delegate types:"); - DelegateEquality(); - IdenticalLambdasNotEqual(); - - Console.WriteLine("Non equality:"); - NonEquality(); - } - - private static void ValueTypesEquality() - { - // - int a = 1 + 2 + 3; - int b = 6; - Console.WriteLine(a == b); // output: True - - char c1 = 'a'; - char c2 = 'A'; - Console.WriteLine(c1 == c2); // output: False - Console.WriteLine(c1 == char.ToLower(c2)); // output: True - // - } - - private static void StringEquality() - { - // - string s1 = "hello!"; - string s2 = "HeLLo!"; - Console.WriteLine(s1 == s2.ToLower()); // output: True - - string s3 = "Hello!"; - Console.WriteLine(s1 == s3); // output: False - // - } - - // Rationale for the structure of the next snippet. - // A method cannot contain a class definition. Thus, a standard way to include snippet doesn't work. - // We want snippets to be interactive. Thus, the whole snippet has a structure of the console program. - // (Running the code without the ReferenceTypesEquality class doesn't produce any output in the interactive Output control.) - - // - public class ReferenceTypesEquality - { - public class MyClass - { - private int id; - - public MyClass(int id) => this.id = id; - } - - public static void Main() - { - var a = new MyClass(1); - var b = new MyClass(1); - var c = a; - Console.WriteLine(a == b); // output: False - Console.WriteLine(a == c); // output: True - } - } - // - - private static void NonEquality() - { - // - int a = 1 + 1 + 2 + 3; - int b = 6; - Console.WriteLine(a != b); // output: True - - string s1 = "Hello"; - string s2 = "Hello"; - Console.WriteLine(s1 != s2); // output: False - - object o1 = 1; - object o2 = 1; - Console.WriteLine(o1 != o2); // output: True - // - } - - private static void DelegateEquality() - { - // - Action a = () => Console.WriteLine("a"); - - Action b = a + a; - Action c = a + a; - Console.WriteLine(object.ReferenceEquals(b, c)); // output: False - Console.WriteLine(b == c); // output: True - // - } - - private static void IdenticalLambdasNotEqual() - { - // - Action a = () => Console.WriteLine("a"); - Action b = () => Console.WriteLine("a"); - - Console.WriteLine(a == b); // output: False - Console.WriteLine(a + b == a + b); // output: True - Console.WriteLine(b + a == a + b); // output: False - // - } - } -} \ No newline at end of file diff --git a/csharp/language-reference/operators/LambdaOperator.cs b/csharp/language-reference/operators/LambdaOperator.cs deleted file mode 100644 index f2e4237fb27..00000000000 --- a/csharp/language-reference/operators/LambdaOperator.cs +++ /dev/null @@ -1,47 +0,0 @@ -using System; -using System.Linq; - -namespace operators -{ - public static class LambdaOperator - { - public static void Examples() - { - WithInferredTypes(); - WithExplicitTypes(); - WithoutInput(); - } - - private static void WithInferredTypes() - { - // - string[] words = { "bot", "apple", "apricot" }; - int minimalLength = words - .Where(w => w.StartsWith("a")) - .Min(w => w.Length); - Console.WriteLine(minimalLength); // output: 5 - - int[] numbers = { 1, 4, 7, 10 }; - int product = numbers.Aggregate(1, (interim, next) => interim * next); - Console.WriteLine(product); // output: 280 - // - } - - private static void WithExplicitTypes() - { - // - int[] numbers = { 1, 4, 7, 10 }; - int product = numbers.Aggregate(1, (int interim, int next) => interim * next); - Console.WriteLine(product); // output: 280 - // - } - - private static void WithoutInput() - { - // - Func greet = () => "Hello, World!"; - Console.WriteLine(greet()); - // - } - } -} \ No newline at end of file diff --git a/csharp/language-reference/operators/MemberAccessOperators.cs b/csharp/language-reference/operators/MemberAccessOperators.cs deleted file mode 100644 index ca304c337ef..00000000000 --- a/csharp/language-reference/operators/MemberAccessOperators.cs +++ /dev/null @@ -1,174 +0,0 @@ -using System; -// -using System.Collections.Generic; -// -using System.Linq; - -namespace operators -{ - public static class MemberAccessOperators - { - public static void Examples() - { - TypeMemberAccess(); - Arrays(); - Indexers(); - NullConditional(); - Invocation(); - IndexFromEnd(); - Ranges(); - RangesOptional(); - } - - private static void QualifiedName() - { - // - System.Collections.Generic.IEnumerable numbers = new int[] { 1, 2, 3 }; - // - } - - private static void TypeMemberAccess() - { - // - var constants = new List(); - constants.Add(Math.PI); - constants.Add(Math.E); - Console.WriteLine($"{constants.Count} values to show:"); - Console.WriteLine(string.Join(", ", constants)); - // Output: - // 2 values to show: - // 3.14159265358979, 2.71828182845905 - // - } - - private static void Arrays() - { - // - int[] fib = new int[10]; - fib[0] = fib[1] = 1; - for (int i = 2; i < fib.Length; i++) - { - fib[i] = fib[i - 1] + fib[i - 2]; - } - Console.WriteLine(fib[fib.Length - 1]); // output: 55 - - double[,] matrix = new double[2,2]; - matrix[0,0] = 1.0; - matrix[0,1] = 2.0; - matrix[1,0] = matrix[1,1] = 3.0; - var determinant = matrix[0,0] * matrix[1,1] - matrix[1,0] * matrix[0,1]; - Console.WriteLine(determinant); // output: -3 - // - } - - private static void Indexers() - { - // - var dict = new Dictionary(); - dict["one"] = 1; - dict["pi"] = Math.PI; - Console.WriteLine(dict["one"] + dict["pi"]); // output: 4.14159265358979 - // - } - - private static void NullConditional() - { - // - double SumNumbers(List setsOfNumbers, int indexOfSetToSum) - { - return setsOfNumbers?[indexOfSetToSum]?.Sum() ?? double.NaN; - } - - var sum1 = SumNumbers(null, 0); - Console.WriteLine(sum1); // output: NaN - - var numberSets = new List - { - new[] { 1.0, 2.0, 3.0 }, - null - }; - - var sum2 = SumNumbers(numberSets, 0); - Console.WriteLine(sum2); // output: 6 - - var sum3 = SumNumbers(numberSets, 1); - Console.WriteLine(sum3); // output: NaN - // - } - - private static void Invocation() - { - // - Action display = s => Console.WriteLine(s); - - var numbers = new List(); - numbers.Add(10); - numbers.Add(17); - display(numbers.Count); // output: 2 - - numbers.Clear(); - display(numbers.Count); // output: 0 - // - } - - private static void IndexFromEnd() - { - // - int[] xs = new[] { 0, 10, 20, 30, 40 }; - int last = xs[^1]; - Console.WriteLine(last); // output: 40 - - var lines = new List { "one", "two", "three", "four" }; - string prelast = lines[^2]; - Console.WriteLine(prelast); // output: three - - string word = "Twenty"; - Index toFirst = ^word.Length; - char first = word[toFirst]; - Console.WriteLine(first); // output: T - // - } - - private static void Ranges() - { - // - int[] numbers = new[] { 0, 10, 20, 30, 40, 50 }; - int start = 1; - int amountToTake = 3; - int[] subset = numbers[start..(start + amountToTake)]; - Display(subset); // output: 10 20 30 - - int margin = 1; - int[] inner = numbers[margin..^margin]; - Display(inner); // output: 10 20 30 40 - - string line = "one two three"; - int amountToTakeFromEnd = 5; - Range endIndices = ^amountToTakeFromEnd..^0; - string end = line[endIndices]; - Console.WriteLine(end); // output: three - - void Display(IEnumerable xs) => Console.WriteLine(string.Join(" ", xs)); - // - } - - private static void RangesOptional() - { - // - int[] numbers = new[] { 0, 10, 20, 30, 40, 50 }; - int amountToDrop = numbers.Length / 2; - - int[] rightHalf = numbers[amountToDrop..]; - Display(rightHalf); // output: 30 40 50 - - int[] leftHalf = numbers[..^amountToDrop]; - Display(leftHalf); // output: 0 10 20 - - int[] all = numbers[..]; - Display(all); // output: 0 10 20 30 40 50 - - void Display(IEnumerable xs) => Console.WriteLine(string.Join(" ", xs)); - // - } - } -} \ No newline at end of file diff --git a/csharp/language-reference/operators/NameOfOperator.cs b/csharp/language-reference/operators/NameOfOperator.cs deleted file mode 100644 index a19459769e9..00000000000 --- a/csharp/language-reference/operators/NameOfOperator.cs +++ /dev/null @@ -1,36 +0,0 @@ -using System; -using System.Collections.Generic; - -namespace operators -{ - public static class NameOfOperator - { - public static void Examples() - { - // - Console.WriteLine(nameof(System.Collections.Generic)); // output: Generic - Console.WriteLine(nameof(List)); // output: List - Console.WriteLine(nameof(List.Count)); // output: Count - Console.WriteLine(nameof(List.Add)); // output: Add - - var numbers = new List { 1, 2, 3 }; - Console.WriteLine(nameof(numbers)); // output: numbers - Console.WriteLine(nameof(numbers.Count)); // output: Count - Console.WriteLine(nameof(numbers.Add)); // output: Add - // - } - - private class Person - { - string name; - - // - public string Name - { - get => name; - set => name = value ?? throw new ArgumentNullException(nameof(value), $"{nameof(Name)} cannot be null"); - } - // - } - } -} \ No newline at end of file diff --git a/csharp/language-reference/operators/NewOperator.cs b/csharp/language-reference/operators/NewOperator.cs deleted file mode 100644 index cde07b22950..00000000000 --- a/csharp/language-reference/operators/NewOperator.cs +++ /dev/null @@ -1,82 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; - -namespace operators -{ - public static class NewOperator - { - public static void Examples() - { - Constructor(); - ConstructorWithInitializer(); - Array(); - ArrayInitialization(); - AnonymousType(); - } - - private static void Constructor() - { - // - var dict = new Dictionary(); - dict["first"] = 10; - dict["second"] = 20; - dict["third"] = 30; - - Console.WriteLine(string.Join("; ", dict.Select(entry => $"{entry.Key}: {entry.Value}"))); - // Output: - // first: 10; second: 20; third: 30 - // - } - - private static void ConstructorWithInitializer() - { - // - var dict = new Dictionary - { - ["first"] = 10, - ["second"] = 20, - ["third"] = 30 - }; - - Console.WriteLine(string.Join("; ", dict.Select(entry => $"{entry.Key}: {entry.Value}"))); - // Output: - // first: 10; second: 20; third: 30 - // - } - - private static void Array() - { - // - var numbers = new int[3]; - numbers[0] = 10; - numbers[1] = 20; - numbers[2] = 30; - - Console.WriteLine(string.Join(", ", numbers)); - // Output: - // 10, 20, 30 - // - } - - private static void ArrayInitialization() - { - // - var a = new int[3] { 10, 20, 30 }; - var b = new int[] { 10, 20, 30 }; - var c = new[] { 10, 20, 30 }; - Console.WriteLine(c.GetType()); // output: System.Int32[] - // - } - - private static void AnonymousType() - { - // - var example = new { Greeting = "Hello", Name = "World" }; - Console.WriteLine($"{example.Greeting}, {example.Name}!"); - // Output: - // Hello, World! - // - } - } -} \ No newline at end of file diff --git a/csharp/language-reference/operators/NullCoalescingOperator.cs b/csharp/language-reference/operators/NullCoalescingOperator.cs deleted file mode 100644 index 1a4699f2bf4..00000000000 --- a/csharp/language-reference/operators/NullCoalescingOperator.cs +++ /dev/null @@ -1,73 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; - -namespace operators -{ - public static class NullCoalescingOperator - { - public static void Examples() - { - WithNullConditional(); - WithNullableTypes(); - NullCoalescingAssignment(); - } - - private static void WithNullConditional() - { - // - double SumNumbers(List setsOfNumbers, int indexOfSetToSum) - { - return setsOfNumbers?[indexOfSetToSum]?.Sum() ?? double.NaN; - } - - var sum = SumNumbers(null, 0); - Console.WriteLine(sum); // output: NaN - // - } - - private static void WithNullableTypes() - { - // - int? a = null; - int b = a ?? -1; - Console.WriteLine(b); // output: -1 - // - } - - private class Person - { - string name; - - // - public string Name - { - get => name; - set => name = value ?? throw new ArgumentNullException(nameof(value), "Name cannot be null"); - } - // - } - - // - private static void Display(T a, T backup) - { - Console.WriteLine(a ?? backup); - } - // - - private static void NullCoalescingAssignment() - { - // - List numbers = null; - int? a = null; - - (numbers ??= new List()).Add(5); - Console.WriteLine(string.Join(" ", numbers)); // output: 5 - - numbers.Add(a ??= 0); - Console.WriteLine(string.Join(" ", numbers)); // output: 5 0 - Console.WriteLine(a); // output: 0 - // - } - } -} \ No newline at end of file diff --git a/csharp/language-reference/operators/NullForgivingOperator.cs b/csharp/language-reference/operators/NullForgivingOperator.cs deleted file mode 100644 index 3589f40b27c..00000000000 --- a/csharp/language-reference/operators/NullForgivingOperator.cs +++ /dev/null @@ -1,70 +0,0 @@ -using System; -using System.Diagnostics.CodeAnalysis; -using Microsoft.VisualStudio.TestTools.UnitTesting; - -namespace operators -{ - // - #nullable enable - public class Person - { - public Person(string name) => Name = name ?? throw new ArgumentNullException(nameof(name)); - - public string Name { get; } - } - // - - [TestClass] - public class PersonTests - { - // - [TestMethod, ExpectedException(typeof(ArgumentNullException))] - public void NullNameShouldThrowTest() - { - var person = new Person(null!); - } - // - } - - public static class UseNullForgivingExample - { - // - public static void Main() - { - Person? p = Find("John"); - if (IsValid(p)) - { - Console.WriteLine($"Found {p!.Name}"); - } - } - - public static bool IsValid(Person? person) - { - return person != null && !string.IsNullOrEmpty(person.Name); - } - // - - public static Person? Find(string name) => null; - } - - public static class UseAttributeExample - { - // - public static void Main() - { - Person? p = Find("John"); - if (IsValid(p)) - { - Console.WriteLine($"Found {p.Name}"); - } - } - - public static bool IsValid([NotNullWhen(true)] Person? person) - { - return person != null && !string.IsNullOrEmpty(person.Name); - } - // - - public static Person? Find(string name) => null; - } -} diff --git a/csharp/language-reference/operators/OperatorOverloading.cs b/csharp/language-reference/operators/OperatorOverloading.cs deleted file mode 100644 index 40a601a38d9..00000000000 --- a/csharp/language-reference/operators/OperatorOverloading.cs +++ /dev/null @@ -1,54 +0,0 @@ -using System; - -public readonly struct Fraction -{ - private readonly int num; - private readonly int den; - - public Fraction(int numerator, int denominator) - { - if (denominator == 0) - { - throw new ArgumentException("Denominator cannot be zero.", nameof(denominator)); - } - num = numerator; - den = denominator; - } - - public static Fraction operator +(Fraction a) => a; - public static Fraction operator -(Fraction a) => new Fraction(-a.num, a.den); - - public static Fraction operator +(Fraction a, Fraction b) - => new Fraction(a.num * b.den + b.num * a.den, a.den * b.den); - - public static Fraction operator -(Fraction a, Fraction b) - => a + (-b); - - public static Fraction operator *(Fraction a, Fraction b) - => new Fraction(a.num * b.num, a.den * b.den); - - public static Fraction operator /(Fraction a, Fraction b) - { - if (b.num == 0) - { - throw new DivideByZeroException(); - } - return new Fraction(a.num * b.den, a.den * b.num); - } - - public override string ToString() => $"{num} / {den}"; -} - -public static class OperatorOverloading -{ - public static void Main() - { - var a = new Fraction(5, 4); - var b = new Fraction(1, 2); - Console.WriteLine(-a); // output: -5 / 4 - Console.WriteLine(a + b); // output: 14 / 8 - Console.WriteLine(a - b); // output: 6 / 8 - Console.WriteLine(a * b); // output: 5 / 8 - Console.WriteLine(a / b); // output: 10 / 4 - } -} diff --git a/csharp/language-reference/operators/PointerOperators.cs b/csharp/language-reference/operators/PointerOperators.cs deleted file mode 100644 index ffdeb5dc284..00000000000 --- a/csharp/language-reference/operators/PointerOperators.cs +++ /dev/null @@ -1,170 +0,0 @@ -using System; - -namespace operators -{ - public static class PointerOperators - { - public static void Examples() - { - AddressOf(); - PointerIndirection(); - PointerMemberAccessExample.Main(); - PointerElementAccess(); - PointerAddition(); - PointerSubtraction(); - Increment(); - } - - private static void AddressOf() - { - // - unsafe - { - int number = 27; - int* pointerToNumber = &number; - - Console.WriteLine($"Value of the variable: {number}"); - Console.WriteLine($"Address of the variable: {(long)pointerToNumber:X}"); - } - // Output is similar to: - // Value of the variable: 27 - // Address of the variable: 6C1457DBD4 - // - } - - private static void AddressOfFixed() - { - // - unsafe - { - byte[] bytes = { 1, 2, 3 }; - fixed (byte* pointerToFirst = &bytes[0]) - { - // The address stored in pointerToFirst - // is valid only inside this fixed statement block. - } - } - // - } - - private static void PointerIndirection() - { - // - unsafe - { - char letter = 'A'; - char* pointerToLetter = &letter; - Console.WriteLine($"Value of the `letter` variable: {letter}"); - Console.WriteLine($"Address of the `letter` variable: {(long)pointerToLetter:X}"); - - *pointerToLetter = 'Z'; - Console.WriteLine($"Value of the `letter` variable after update: {letter}"); - } - // Output is similar to: - // Value of the `letter` variable: A - // Address of the `letter` variable: DCB977DDF4 - // Value of the `letter` variable after update: Z - // - } - - // - public struct Coords - { - public int X; - public int Y; - public override string ToString() => $"({X}, {Y})"; - } - - public class PointerMemberAccessExample - { - public static unsafe void Main() - { - Coords coords; - Coords* p = &coords; - p->X = 3; - p->Y = 4; - Console.WriteLine(p->ToString()); // output: (3, 4) - } - } - // - - private static void PointerElementAccess() - { - // - unsafe - { - char* pointerToChars = stackalloc char[123]; - - for (int i = 65; i < 123; i++) - { - pointerToChars[i] = (char)i; - } - - Console.Write("Uppercase letters: "); - for (int i = 65; i < 91; i++) - { - Console.Write(pointerToChars[i]); - } - } - // Output: - // Uppercase letters: ABCDEFGHIJKLMNOPQRSTUVWXYZ - // - Console.WriteLine(); - } - - private static void PointerAddition() - { - // - unsafe - { - const int Count = 3; - int[] numbers = new int[Count] { 10, 20, 30 }; - fixed (int* pointerToFirst = &numbers[0]) - { - int* pointerToLast = pointerToFirst + (Count - 1); - - Console.WriteLine($"Value {*pointerToFirst} at address {(long)pointerToFirst}"); - Console.WriteLine($"Value {*pointerToLast} at address {(long)pointerToLast}"); - } - } - // Output is similar to: - // Value 10 at address 1818345918136 - // Value 30 at address 1818345918144 - // - } - - private static void PointerSubtraction() - { - // - unsafe - { - int* numbers = stackalloc int[] { 0, 1, 2, 3, 4, 5 }; - int* p1 = &numbers[1]; - int* p2 = &numbers[5]; - Console.WriteLine(p2 - p1); // output: 4 - } - // - } - - private static void Increment() - { - // - unsafe - { - int* numbers = stackalloc int[] { 0, 1, 2 }; - int* p1 = &numbers[0]; - int* p2 = p1; - Console.WriteLine($"Before operation: p1 - {(long)p1}, p2 - {(long)p2}"); - Console.WriteLine($"Postfix increment of p1: {(long)(p1++)}"); - Console.WriteLine($"Prefix increment of p2: {(long)(++p2)}"); - Console.WriteLine($"After operation: p1 - {(long)p1}, p2 - {(long)p2}"); - } - // Output is similar to - // Before operation: p1 - 816489946512, p2 - 816489946512 - // Postfix increment of p1: 816489946512 - // Prefix increment of p2: 816489946516 - // After operation: p1 - 816489946516, p2 - 816489946516 - // - } - } -} diff --git a/csharp/language-reference/operators/Program.cs b/csharp/language-reference/operators/Program.cs deleted file mode 100644 index 4c0b7ae9dd0..00000000000 --- a/csharp/language-reference/operators/Program.cs +++ /dev/null @@ -1,107 +0,0 @@ -using System; -using System.Threading.Tasks; - -namespace operators -{ - class Program - { - static async Task Main(string[] args) - { - Console.WriteLine("======== Arithmetic operators examples ========="); - ArithmeticOperators.Examples(); - Console.WriteLine(); - - Console.WriteLine("============= == and != operators examples ====="); - EqualityOperators.Examples(); - Console.WriteLine(); - - Console.WriteLine("======== Logical operators examples ============"); - BooleanLogicalOperators.Examples(); - Console.WriteLine(); - - Console.WriteLine("==== Bitwise and shift operators examples ======"); - BitwiseAndShiftOperators.Examples(); - Console.WriteLine(); - - Console.WriteLine("====== >, <, >=, and <= operators examples ====="); - ComparisonOperators.Examples(); - Console.WriteLine(); - - Console.WriteLine("========= Member access operators examples ====="); - MemberAccessOperators.Examples(); - Console.WriteLine(); - - Console.WriteLine("======= Pointer related operators examples ====="); - PointerOperators.Examples(); - Console.WriteLine(); - - Console.WriteLine("= Type-testing and conversion operators examples"); - TypeTestingAndConversionOperators.Examples(); - Console.WriteLine(); - - Console.WriteLine("============== = operator examples ============="); - AssignmentOperator.Examples(); - Console.WriteLine(); - - Console.WriteLine("============== + operator examples ============="); - AdditionOperator.Examples(); - Console.WriteLine(); - - Console.WriteLine("============== - operator examples ============="); - SubtractionOperator.Examples(); - Console.WriteLine(); - - Console.WriteLine("============== ?: operator examples ============"); - ConditionalOperator.Examples(); - Console.WriteLine(); - - Console.WriteLine("========== ?? and ??= operators examples ======="); - NullCoalescingOperator.Examples(); - Console.WriteLine(); - - Console.WriteLine("============== => operator examples ============"); - LambdaOperator.Examples(); - Console.WriteLine(); - - Console.WriteLine("=============== await operator examples ========"); - await AwaitOperator.Main(); - Console.WriteLine(); - - Console.WriteLine("=========== default operator examples =========="); - DefaultOperator.Examples(); - Console.WriteLine(); - - Console.WriteLine("========== delegate operator examples =========="); - DelegateOperator.Examples(); - Console.WriteLine(); - - Console.WriteLine("=========== nameof operator examples ==========="); - NameOfOperator.Examples(); - Console.WriteLine(); - - Console.WriteLine("============= new operator examples ============"); - NewOperator.Examples(); - Console.WriteLine(); - - Console.WriteLine("=============== sizeof operator examples ======="); - SizeOfOperator.Main(); - Console.WriteLine(); - - Console.WriteLine("============ stackalloc operator examples ======"); - StackallocOperator.Examples(); - Console.WriteLine(); - - Console.WriteLine("========= true and false operators examples ===="); - LaunchStatusTest.Main(); - Console.WriteLine(); - - Console.WriteLine("========= operator overloading example ========="); - OperatorOverloading.Main(); - Console.WriteLine(); - - Console.WriteLine("========= conversion operators example ========="); - UserDefinedConversions.Main(); - Console.WriteLine(); - } - } -} diff --git a/csharp/language-reference/operators/SizeOfOperator.cs b/csharp/language-reference/operators/SizeOfOperator.cs deleted file mode 100644 index c3ee6646539..00000000000 --- a/csharp/language-reference/operators/SizeOfOperator.cs +++ /dev/null @@ -1,32 +0,0 @@ -using System; - -public struct Point -{ - public Point(byte tag, double x, double y) => (Tag, X, Y) = (tag, x, y); - - public byte Tag { get; } - public double X { get; } - public double Y { get; } -} - -public class SizeOfOperator -{ - public static void Main() - { - Console.WriteLine(sizeof(byte)); // output: 1 - Console.WriteLine(sizeof(double)); // output: 8 - - DisplaySizeOf(); // output: Size of Point is 24 - DisplaySizeOf(); // output: Size of System.Decimal is 16 - - unsafe - { - Console.WriteLine(sizeof(Point*)); // output: 8 - } - } - - static unsafe void DisplaySizeOf() where T : unmanaged - { - Console.WriteLine($"Size of {typeof(T)} is {sizeof(T)}"); - } -} diff --git a/csharp/language-reference/operators/StackallocOperator.cs b/csharp/language-reference/operators/StackallocOperator.cs deleted file mode 100644 index 91da0d0b332..00000000000 --- a/csharp/language-reference/operators/StackallocOperator.cs +++ /dev/null @@ -1,65 +0,0 @@ -using System; - -namespace operators -{ - public static class StackallocOperator - { - public static void Examples() - { - StackallocInNestedExpressions(); - } - - private static void AssignToPointer() - { - // - unsafe - { - int length = 3; - int* numbers = stackalloc int[length]; - for (var i = 0; i < length; i++) - { - numbers[i] = i; - } - } - // - } - - private static void AssignToSpan() - { - // - int length = 3; - Span numbers = stackalloc int[length]; - for (var i = 0; i < length; i++) - { - numbers[i] = i; - } - // - } - - private static void AsExpression() - { - // - int length = 1000; - Span buffer = length <= 1024 ? stackalloc byte[length] : new byte[length]; - // - } - - private static void StackallocInit() - { - // - Span first = stackalloc int[3] { 1, 2, 3 }; - Span second = stackalloc int[] { 1, 2, 3 }; - ReadOnlySpan third = stackalloc[] { 1, 2, 3 }; - // - } - - private static void StackallocInNestedExpressions() - { - // - Span numbers = stackalloc[] { 1, 2, 3, 4, 5, 6 }; - var ind = numbers.IndexOfAny(stackalloc[] { 2, 4, 6 ,8 }); - Console.WriteLine(ind); // output: 1 - // - } - } -} diff --git a/csharp/language-reference/operators/SubtractionOperator.cs b/csharp/language-reference/operators/SubtractionOperator.cs deleted file mode 100644 index e02859ebd1b..00000000000 --- a/csharp/language-reference/operators/SubtractionOperator.cs +++ /dev/null @@ -1,95 +0,0 @@ -using System; - -namespace operators -{ - public static class SubtractionOperator - { - public static void Examples() - { - DelegateRemoval(); - DelegateRemovalNoChange(); - DelegateRemovalAndNull(); - 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 nihil = abbaab - abbaab; - Console.WriteLine(nihil is null); // output: True - // - } - - private static void DelegateRemovalNoChange() - { - // - Action a = () => Console.Write("a"); - Action b = () => Console.Write("b"); - - var abbaab = a + b + b + a + a + b; - var aba = a + b + a; - - var first = abbaab - aba; - first(); // output: abbaab - Console.WriteLine(); - Console.WriteLine(object.ReferenceEquals(abbaab, first)); // output: True - - Action a2 = () => Console.Write("a"); - var changed = aba - a; - changed(); // output: ab - Console.WriteLine(); - var unchanged = aba - a2; - unchanged(); // output: aba - Console.WriteLine(); - Console.WriteLine(object.ReferenceEquals(aba, unchanged)); // output: True - // - } - - private static void DelegateRemovalAndNull() - { - // - Action a = () => Console.Write("a"); - - var nothing = null - a; - Console.WriteLine(nothing is null); // output: True - - var first = a - null; - a(); // output: a - Console.WriteLine(); - Console.WriteLine(object.ReferenceEquals(first, a)); // 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/TrueFalseOperators.cs b/csharp/language-reference/operators/TrueFalseOperators.cs deleted file mode 100644 index 79a9fcf4d25..00000000000 --- a/csharp/language-reference/operators/TrueFalseOperators.cs +++ /dev/null @@ -1,60 +0,0 @@ -using System; - -public struct LaunchStatus -{ - public static readonly LaunchStatus Green = new LaunchStatus(0); - public static readonly LaunchStatus Yellow = new LaunchStatus(1); - public static readonly LaunchStatus Red = new LaunchStatus(2); - - private int status; - - private LaunchStatus(int status) - { - this.status = status; - } - - public static bool operator true(LaunchStatus x) => x == Green || x == Yellow; - public static bool operator false(LaunchStatus x) => x == Red; - - public static LaunchStatus operator &(LaunchStatus x, LaunchStatus y) - { - if (x == Red || y == Red || (x == Yellow && y == Yellow)) - { - return Red; - } - - if (x == Yellow || y == Yellow) - { - return Yellow; - } - - return Green; - } - - public static bool operator ==(LaunchStatus x, LaunchStatus y) => x.status == y.status; - public static bool operator !=(LaunchStatus x, LaunchStatus y) => !(x == y); - - public override bool Equals(object obj) => obj is LaunchStatus other && this == other; - public override int GetHashCode() => status; -} - -public class LaunchStatusTest -{ - public static void Main() - { - LaunchStatus okToLaunch = GetFuelLaunchStatus() && GetNavigationLaunchStatus(); - Console.WriteLine(okToLaunch ? "Ready to go!" : "Wait!"); - } - - static LaunchStatus GetFuelLaunchStatus() - { - Console.WriteLine("Getting fuel launch status..."); - return LaunchStatus.Red; - } - - static LaunchStatus GetNavigationLaunchStatus() - { - Console.WriteLine("Getting navigation launch status..."); - return LaunchStatus.Yellow; - } -} diff --git a/csharp/language-reference/operators/TypeTestingAndConversionOperators.cs b/csharp/language-reference/operators/TypeTestingAndConversionOperators.cs deleted file mode 100644 index 3c630b0efd2..00000000000 --- a/csharp/language-reference/operators/TypeTestingAndConversionOperators.cs +++ /dev/null @@ -1,136 +0,0 @@ -using System; -using System.Collections.Generic; - -namespace operators -{ - public static class TypeTestingAndConversionOperators - { - public static void Examples() - { - Cast(); - IsOperatorExample.Main(); - IsOperatorWithInt(); - IsOperatorTypePattern(); - AsOperator(); - TypeOf(); - TypeOfUnboundGeneric(); - TypeOfExample.Main(); - } - - private static void Cast() - { - // - double x = 1234.7; - int a = (int)x; - Console.WriteLine(a); // output: 1234 - - IEnumerable numbers = new int[] { 10, 20, 30 }; - IList list = (IList)numbers; - Console.WriteLine(list.Count); // output: 3 - Console.WriteLine(list[1]); // output: 20 - // - } - - // - public class Base { } - - public class Derived : Base { } - - public static class IsOperatorExample - { - public static void Main() - { - object b = new Base(); - Console.WriteLine(b is Base); // output: True - Console.WriteLine(b is Derived); // output: False - - object d = new Derived(); - Console.WriteLine(d is Base); // output: True - Console.WriteLine(d is Derived); // output: True - } - } - // - - private static void IsOperatorWithInt() - { - // - int i = 27; - Console.WriteLine(i is System.IFormattable); // output: True - - object iBoxed = i; - Console.WriteLine(iBoxed is int); // output: True - Console.WriteLine(iBoxed is long); // output: False - // - } - - private static void IsOperatorTypePattern() - { - // - int i = 23; - object iBoxed = i; - int? jNullable = 7; - if (iBoxed is int a && jNullable is int b) - { - Console.WriteLine(a + b); // output 30 - } - // - } - - private static void AsOperator() - { - // - IEnumerable numbers = new[] { 10, 20, 30 }; - IList indexable = numbers as IList; - if (indexable != null) - { - Console.WriteLine(indexable[0] + indexable[indexable.Count - 1]); // output: 40 - } - // - } - - private static void TypeOf() - { - // - void PrintType() => Console.WriteLine(typeof(T)); - - Console.WriteLine(typeof(List)); - PrintType(); - PrintType(); - PrintType>(); - // Output: - // System.Collections.Generic.List`1[System.String] - // System.Int32 - // System.Int32 - // System.Collections.Generic.Dictionary`2[System.Int32,System.Char] - // - } - - private static void TypeOfUnboundGeneric() - { - // - Console.WriteLine(typeof(Dictionary<,>)); - // Output: - // System.Collections.Generic.Dictionary`2[TKey,TValue] - // - } - - // - public class Animal { } - - public class Giraffe : Animal { } - - public static class TypeOfExample - { - public static void Main() - { - object b = new Giraffe(); - Console.WriteLine(b is Animal); // output: True - Console.WriteLine(b.GetType() == typeof(Animal)); // output: False - - Console.WriteLine(b is Giraffe); // output: True - Console.WriteLine(b.GetType() == typeof(Giraffe)); // output: True - } - } - // - } -} \ No newline at end of file diff --git a/csharp/language-reference/operators/UserDefinedConversions.cs b/csharp/language-reference/operators/UserDefinedConversions.cs deleted file mode 100644 index 4bac99f59a3..00000000000 --- a/csharp/language-reference/operators/UserDefinedConversions.cs +++ /dev/null @@ -1,34 +0,0 @@ -using System; - -public readonly struct Digit -{ - private readonly byte digit; - - public Digit(byte digit) - { - if (digit > 9) - { - throw new ArgumentOutOfRangeException(nameof(digit), "Digit cannot be greater than nine."); - } - this.digit = digit; - } - - public static implicit operator byte(Digit d) => d.digit; - public static explicit operator Digit(byte b) => new Digit(b); - - public override string ToString() => $"{digit}"; -} - -public static class UserDefinedConversions -{ - public static void Main() - { - var d = new Digit(7); - - byte number = d; - Console.WriteLine(number); // output: 7 - - Digit digit = (Digit)number; - Console.WriteLine(digit); // output: 7 - } -} diff --git a/csharp/language-reference/operators/operators.csproj b/csharp/language-reference/operators/operators.csproj deleted file mode 100644 index baaac1d06e7..00000000000 --- a/csharp/language-reference/operators/operators.csproj +++ /dev/null @@ -1,15 +0,0 @@ - - - - Exe - netcoreapp3.1 - 8.0 - operators.Program - true - - - - - - -