Introducing Radical.sh

Forget Code launches a powerful code generator for building API's

Comparing strings in C#

We can compare the strings by a simple if condition.
Remember to use 'Equals' instead of '=' or '= =' for comparing strings.

  1. using System;
  2. using System.IO;
  3. using System.Text;
  4.  
  5. namespace forgetCode
  6. {
  7. class program1
  8. {
  9. static void Main(string[] args)
  10. {
  11. string firstString = "Forget code";
  12. string secondString = "Forget code";
  13.  
  14. if (firstString.Equals(secondString))
  15. {
  16. Console.WriteLine("Strings are same");
  17. }
  18. else
  19. {
  20. Console.WriteLine("Strings are not same");
  21. }
  22. }
  23. }
  24. }


Output:
Strings are same

..

  1. using System;
  2. using System.IO;
  3. using System.Text;
  4.  
  5. namespace forgetCode
  6. {
  7. class program1
  8. {
  9. static void Main(string[] args)
  10. {
  11. string firstString = "Forget code";
  12. string secondString = "Forget";
  13.  
  14. if (firstString.Equals(secondString))
  15. {
  16. Console.WriteLine("Strings are same");
  17. }
  18. else
  19. {
  20. Console.WriteLine("Strings are not same");
  21. }
  22. }
  23. }
  24. }


Output:
Strings are not same

..