C# | CompareOrdinal() Method

31 May, 2022
In C#, CompareOrdinal() is a string method. This method is used to compare the two specified string objects or substrings using the numerical values of the corresponding Char objects in each string or substring. This method can be overloaded by passing different parameters to it. 
CompareOrdinal(String, String)
CompareOrdinal(String, Int32, String, Int32, Int32)
 
CompareOrdinal(String, String)
This method is used to compare the two particular String objects by calculating the numeric values of the corresponding Char objects in each string.Syntax:  
public static int CompareOrdinal(
    string strA, string strB)
 
Parameters: This method accepts two parameters strA and strB. StrA is the first string to be compare and strB is the second string to be compare. The type of both the parameters is System.String.
Return Value: This method returns a integer value of type System.Int32. If both strings are equal, it returns 0. It returns positive number if the first string is greater than second string, otherwise, it returns the negative number.
Example:  
Input: 
              string s1 = "GFG";    
              string s2 = "GFG"; 

              string.CompareOrdinal(s1, s2)
Output: 0

Input: 
              string s1 = "hello";    
              string s2 = "csharp"; 
              string.CompareOrdinal(s1, s2)

Output: 5

Input: 
              string s1 = "csharp";    
              string s2 = "mello";
 
              string.CompareOrdinal(s1, s2)

Output: -5
Program: To illustrate the CompareOrdinal(string strA, string strB) method. 
CSharp
// C# program to demonstrate the// CompareOrdinal(string strA, string strB)using System;class Geeks {     // Main Method    public static void Main(string[] args)    {         // strings to be compared        string s1 = "GFG";           string s2 = "GFG";        string s3 = "hello";        string s4 = "csharp";         // using CompareOrdinal(string strA, string strB)        // method to compare displaying resultant value        Console.WriteLine(string.CompareOrdinal(s1, s2));        Console.WriteLine(string.CompareOrdinal(s1, s3));        Console.WriteLine(string.CompareOrdinal(s3, s4));    }}
0
-33
5
 
CompareOrdinal(String, Int32, String, Int32, Int32)
This method is used to compare the substrings of the two particular string objects by calculating the numeric values of the corresponding Char objects in each substring.Syntax:  
public static int CompareOrdinal(
    string strA,
    int indexA,
    string strB,
    int indexB,
    int length
)
Parameters: This method will take the five parameters, strA is the first string object and similarly strB is the second string object and indexA is the starting index of the substring in strA and indexB is the starting index of the substring in strB and length is the maximum number of characters in the substrings to compare. The type of strA and StrB is System.String and indexA, indexB and length is of type System.Int32.Return Value: This method will return a integer value of type System.Int32. This method will return 0 if both the substrings are equal and also the length is zero. It will return a positive value if the substring in the strA is greater than the substring in the strB otherwise returns negative value.Exception: This method will give ArgumentOutOfRangeException in three cases:  
if strA is not null and indexA is greater than the Length of strA.
if indexA, indexB, or length is negative.
if strB is not null and the indexB is greater than the Length of strB.
Program: To illustrate the CompareOrdinal(string strA, int indexA, string strB, int indexB, int length): 
CSharp
// C# program to illustrate the// CompareOrdinal(String, Int32,// String, Int32, Int32) methodusing System; class GFG {         // Main Method    static public void Main ()    {                 // strings to be compared        string s1 = "GeeksforGeeks";           string s2 = "GforG";                 // starting index of substrings        int sub1 = 5;        int sub2 = 1;                 // length (5th parameter)        int l1 = 3;                 // using CompareOrdinal(String, Int32,        // String, Int32, Int32) method and        // storing the result in variable res        int res = string.CompareOrdinal(s1, sub1, s2, sub2, l1);                 // Displaying the result        Console.WriteLine("The Result is: " + res);             }}
The Result is: 0
 
References:  
https://msdn.microsoft.com/en-us/library/af26w0wa(v=vs.110).aspx
 
https://msdn.microsoft.com/en-us/library/es986b3k(v=vs.110).aspx
 
gulshankumarar231
rkbhola5
CSharp-method
CSharp-string
C#
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
C# | Multiple inheritance using interfaces
Differences Between .NET Core and .NET Framework
Extension Method in C#
C# | List Class
HashSet in C# with Examples
C# | .NET Framework (Basic Architecture and Component Stack)
Switch Statement in C#
Partial Classes in C#
Lambda Expressions in C#
Hello World in C#