C# program to compare two strings are equal or not


Following program shows you how to compare two strings are equal or not.
In this program we get strings from user and compare those strings using .Equals() method

using System;

class MainClass {
  public static void Main (string[] args) {
    Console.WriteLine("Please enter first string:");
    string string1 = Console.ReadLine();
    Console.WriteLine("Please enter second string:");
    string string2 = Console.ReadLine();
    if (string1.Equals(string2)) {
    Console.WriteLine("Given strings are equal");
    } else {
      Console.WriteLine("Given strings are not equal");
    }
  }
}

Output:

Example1:

Please enter first string:
 hello
Please enter second string:
 hello
Given strings are equal

Example2:

Please enter first string:
 hello
Please enter second string:
 HELLO
Given strings are not equal