How to use C# string Compare

The CSharp String Compare function compares two strings lexicographically . The comparison is based on the Unicode value of each character in the string.

It returns an Integer indication lexical relationship between the two comprehends


  1. string str1 : Parameter String
  2. string str2 : Parameter String


Integer : returns less than zero, zero or greater than zero.

  1. Less than zero : str1 is less than str2
  2. Zero : str1 is equal to str2
  3. Greater than zero : str1 is greater than str2

using System;
using System.Windows.Forms;

namespace WindowsApplication1
    public partial class Form1 : Form
        public Form1()

        private void button1_Click(object sender, EventArgs e)
            string str1 = null;
            string str2 = null;

            str1 = "csharp";
            str2 = "CSharp";

            int result = 0;

            result = string.Compare(str1, str2);

            result = string.Compare(str1, str2, true);

When you run this C# program first u will get -1 in message box and then 0 (C) 2021    Founded by raps mk
All Rights Reserved. All other trademarks are property of their respective owners.
SiteMap  | Terms  | About