check if string is equal c# code example

Example 1: c sharp if string equals

// There are two ways to check if 2 strings are the same:
string str1 = "Hello";
string str2 = "Hello";

// The first is using double equals sign ==
str1 == str2; // Output: true

// The second is using the 'Equals()' function
str1.Equals(str2); // Output: true

Example 2: c# identical strings not equal

/*
Reflection can use characters that look like other characters, but are actually different!
 
Reflection boxes the values twice into new objects and == will compare by reference. 
Try using object.Equals(currentValue, newValue) instead or convert the objects to strings first.
*/