String Compareto() - C# Tutorial
C# Course / Strings / String Compareto()

String Compareto()

BLUF: Mastering String Compareto() is essential for building robust applications with the .NET ecosystem. This tutorial provides clear explanations and practical examples to help you understand and apply this C# concept.
Enterprise Development Tip: String Compareto()

C# is a powerful, modern language for enterprise solutions. Discover how String Compareto() enhances your development workflow in the guide below.

The CompareTo method in C# is employed to compare a String instance with a specific String object. It determines if this String instance comes before, after, or is at the same position in the sorting order as the specified string.

Signature

Example

public int CompareTo(String str)
public int CompareTo(Object)

Parameters

The "str" parameter is a string argument utilized for comparison purposes.

Return

It returns an integer value.

C# String CompareTo Method Example

Example

using System;  
    public class StringExample  
    {  
        public static void Main(string[] args)  
        {  
            string s1 = "hello";  
            string s2 = "hello";  
            string s3 = "csharp";
            Console.WriteLine(s1.CompareTo(s2)); 
            Console.WriteLine(s2.CompareTo(s3)); 
        }  
    }

Output:

Input Required

This code uses input(). Please provide values below:

Logic Practice
Install Logic Practice
Add to home screen for a faster app-like experience