Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
using System;

namespace operators
{
public static class EqualityAndNonEqualityExamples
{
public static void Examples()
{
Console.WriteLine("Value types:");
ValueTypesEquality();

Console.WriteLine("Strings:");
StringEquality();

Console.WriteLine("Reference types:");
ReferenceTypesEquality.Main();

Console.WriteLine("Non equality:");
NonEquality();
}

private static void ValueTypesEquality()
{
// <SnippetValueTypesEquality>
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
// </SnippetValueTypesEquality>
}

private static void StringEquality()
{
// <SnippetStringEquality>
string s1 = "hello!";
string s2 = "HeLLo!";
Console.WriteLine(s1 == s2.ToLower()); // output: True

string s3 = "Hello!";
Console.WriteLine(s1 == s3); // output: False
// </SnippetStringEquality>
}

// 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.)

// <SnippetReferenceTypesEquality>
public class ReferenceTypesEquality
{
public class MyClass
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The main idea of this snippet is to show that a reference type supports the == operator by default.

{
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
}
}
// </SnippetReferenceTypesEquality>

private static void NonEquality()
{
// <SnippetNonEquality>
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
// </SnippetNonEquality>
}
}
}
4 changes: 4 additions & 0 deletions snippets/csharp/language-reference/operators/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,10 @@ static void Main(string[] args)
Console.WriteLine("============== / operator examples =============");
DivisionExamples.Examples();
Console.WriteLine();

Console.WriteLine("============== == and != operator examples =====");
EqualityAndNonEqualityExamples.Examples();
Console.WriteLine();
}
}
}