Skip to content

Commit eaf6009

Browse files
pkulikovBillWagner
authored andcommitted
Added equality operator snippets (#512)
* Added equality and non-equality snippets * Minor tweakes * Updated comments
1 parent ec53713 commit eaf6009

File tree

2 files changed

+95
-0
lines changed

2 files changed

+95
-0
lines changed
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
using System;
2+
3+
namespace operators
4+
{
5+
public static class EqualityAndNonEqualityExamples
6+
{
7+
public static void Examples()
8+
{
9+
Console.WriteLine("Value types:");
10+
ValueTypesEquality();
11+
12+
Console.WriteLine("Strings:");
13+
StringEquality();
14+
15+
Console.WriteLine("Reference types:");
16+
ReferenceTypesEquality.Main();
17+
18+
Console.WriteLine("Non equality:");
19+
NonEquality();
20+
}
21+
22+
private static void ValueTypesEquality()
23+
{
24+
// <SnippetValueTypesEquality>
25+
int a = 1 + 2 + 3;
26+
int b = 6;
27+
Console.WriteLine(a == b); // output: True
28+
29+
char c1 = 'a';
30+
char c2 = 'A';
31+
Console.WriteLine(c1 == c2); // output: False
32+
Console.WriteLine(c1 == char.ToLower(c2)); // output: True
33+
// </SnippetValueTypesEquality>
34+
}
35+
36+
private static void StringEquality()
37+
{
38+
// <SnippetStringEquality>
39+
string s1 = "hello!";
40+
string s2 = "HeLLo!";
41+
Console.WriteLine(s1 == s2.ToLower()); // output: True
42+
43+
string s3 = "Hello!";
44+
Console.WriteLine(s1 == s3); // output: False
45+
// </SnippetStringEquality>
46+
}
47+
48+
// Rationale for the structure of the next snippet.
49+
// A method cannot contain a class definition. Thus, a standard way to include snippet doesn't work.
50+
// We want snippets to be interactive. Thus, the whole snippet has a structure of the console program.
51+
// (Running the code without the ReferenceTypesEquality class doesn't produce any output in the interactive Output control.)
52+
53+
// <SnippetReferenceTypesEquality>
54+
public class ReferenceTypesEquality
55+
{
56+
public class MyClass
57+
{
58+
private int id;
59+
60+
public MyClass(int id) => this.id = id;
61+
}
62+
63+
public static void Main()
64+
{
65+
var a = new MyClass(1);
66+
var b = new MyClass(1);
67+
var c = a;
68+
Console.WriteLine(a == b); // output: False
69+
Console.WriteLine(a == c); // output: True
70+
}
71+
}
72+
// </SnippetReferenceTypesEquality>
73+
74+
private static void NonEquality()
75+
{
76+
// <SnippetNonEquality>
77+
int a = 1 + 1 + 2 + 3;
78+
int b = 6;
79+
Console.WriteLine(a != b); // output: True
80+
81+
string s1 = "Hello";
82+
string s2 = "Hello";
83+
Console.WriteLine(s1 != s2); // output: False
84+
85+
object o1 = 1;
86+
object o2 = 1;
87+
Console.WriteLine(o1 != o2); // output: True
88+
// </SnippetNonEquality>
89+
}
90+
}
91+
}

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,10 @@ static void Main(string[] args)
4141
Console.WriteLine("============== / operator examples =============");
4242
DivisionExamples.Examples();
4343
Console.WriteLine();
44+
45+
Console.WriteLine("============== == and != operator examples =====");
46+
EqualityAndNonEqualityExamples.Examples();
47+
Console.WriteLine();
4448
}
4549
}
4650
}

0 commit comments

Comments
 (0)