Compare two strings lexicographically in C#

To compare strings in C#, use the compare() method. It compares two strings and returns the following integer values −

If str1 is less than str2, it returns -1.

If str1 is equal to str2, it returns 0.

If str1 is greater than str2, it returns 1.

Set the two strings in the method and compare them −

string.Compare(string1, string2);


You can try to run the following code to compare two strings in C#.

Live Demo

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Demo {
   class MyApplication {
      static void Main(string[] args) {
         string string1 = null;
         string string2 = null;
         string1 = "amit";
         string2 = "Amit";
         int myOutput = 0;
         myOutput = string.Compare(string1, string2);


karthikeya Boyini
karthikeya Boyini

I love programming (: That's all I know