Comparison

Let's compare some strings, shall we? 😅

Equality

A string is a reference type.

Even though, when you compare two strings to see if they are equal to each other, the compiler compares their values (just as value types):

var name = "Daniel";
var secondName = "Daniel";

if (name == secondName) {
    // this is true
}

Also, we can see if they are different with the != operator:

if (name != secondName) {
    // this is false
}

Ignoring Case

But there is a limitation here: what about ignoring the case of the characters?

We could still do the same thing as before with the Equals method:

if(string.Equals(name, secondName))
    Console.WriteLine("these are equal");

But at the same time, we can specify whether or not we want the to ignore the casing of the strings:

var first = "Daniel";
var second = "daniel";
if (first.Equals(second, StringComparison.OrdinalIgnoreCase))
{
    Console.WriteLine("they are equal");
}

So, for non-case-sensitive comparisons, use the equals method and pass a second argument of OrdinalIgnoreCase.

Compare

We can compare strings as well. 😄

The Compare method compares the ASCII codes of each character.

var first = "break";
var second = "Break";

if (String.Compare(first, second) < 0)
    Console.WriteLine("first");