Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • C# Data Types
  • C# Decision Making
  • C# Methods
  • C# Delegates
  • C# Constructors
  • C# Arrays
  • C# ArrayList
  • C# String
  • C# Tuple
  • C# Indexers
  • C# Interface
  • C# Multithreading
  • C# Exception
Open In App
Next Article:
UInt16.CompareTo() Method in C# with Examples
Next article icon

SByte.CompareTo() Method in C# with Examples

Last Updated : 01 May, 2019
Comments
Improve
Suggest changes
Like Article
Like
Report

SByte.CompareTo() Method is used to compare the current instance to a specified object or SByte and returns an indication of their relative values. There are 2 methods in the overload list of this method as follows:

  • CompareTo(SByte) Method
  • CompareTo(Object) Method

SByte.CompareTo(SByte) Method

This method is used to compare the current instance to a specified 8-bit signed integer and returns an indication of their relative values.

Syntax:

public int CompareTo (sbyte value);

Here, it takes the 8-bit signed integer to compare with the current instance.

Return Value: It returns a 32-bit signed number indicating the relative values of current instance and value parameter as follows:

  • Less than Zero: if Current Instance < value
  • Zero: if Current Instance = value
  • Greater than Zero: if Current Instance > value

Below programs illustrate the use of SByte.CompareTo(SByte) Method:

Example 1:




// C# program to demonstrate the
// SByte.CompareTo(SByte) Method
using System;
  
class GFG {
  
    // Main Method
    public static void Main()
    {
  
        // Declaring and initializing value1
        sbyte value1 = 10;
  
        // Declaring and initializing value2
        sbyte value2 = 20;
  
        // compare both SByte value
        // using CompareTo() method
        int status = value1.CompareTo(value2);
  
        // checking the status
        if (status > 0)
            Console.WriteLine("{0} is greater than {1}",
                                        value1, value2);
        else if (status < 0)
            Console.WriteLine("{0} is less than {1}",
                                      value1, value2);
        else
            Console.WriteLine("{0} is equal to {1}",
                                    value1, value2);
    }
}
 
 
Output:
  10 is less than 20  

Example 2:




// C# program to demonstrate the
// SByte.CompareTo(SByte) Method
using System;
  
class GFG {
  
    // Main Method
    public static void Main()
    {
        // calling get() method
        get(5, 7);
        get(30, 20);
        get(10, 20);
        get(7, -12);
    }
  
    // defining get() method
    public static void get(sbyte value1,
                           sbyte value2)
    {
  
        // using CompareTo() method
        int status = value1.CompareTo(value2);
  
        // checking the status
        if (status > 0)
            Console.WriteLine("{0} is greater than {1}",
                                        value1, value2);
        else if (status < 0)
            Console.WriteLine("{0} is less than {1}",
                                     value1, value2);
        else
            Console.WriteLine("{0} is equal to {1}",
                                    value1, value2);
    }
}
 
 
Output:
  5 is less than 7  30 is greater than 20  10 is less than 20  7 is greater than -12  

SByte.CompareTo(Object) Method

This method is used to compare the current instance to a specified object and returns a comparison of their relative values.

Syntax:

public int CompareTo (object value);

Here, it takes the object to compare with this instance, or null.

Return Value: It returns a 32-bit signed number indicating the relative values of current instance and value parameter as follows:

  • Less than Zero: if Current Instance < value
  • Zero: if Current Instance = value
  • Greater than Zero: if Current Instance > value

Exception: It throws ArgumentException if value is not an SByte.

Below programs illustrate the use of Decimal.CompareTo(Object) Method

Example 1:




// C# program to demonstrate the
// SByte.CompareTo(object) Method
using System;
  
class GFG {
  
    // Main Method
    public static void Main()
    {
        try {
  
            // Declaring and initializing value1
            sbyte value1 = 10;
  
            // Declaring and initializing value2
            // explicit type casting the value 9
            object value2 = (sbyte)9;
  
            // compare both sbyte value
            // using CompareTo() method
            int status = value1.CompareTo(value2);
  
            // checking the status
            if (status > 0)
                Console.WriteLine("{0} is greater than {1}",
                                            value1, value2);
  
            else if (status < 0)
                Console.WriteLine("{0} is less than {1}",
                                         value1, value2);
            else
                Console.WriteLine("{0} is equal to {1}",
                                        value1, value2);
        }
  
        catch (ArgumentException e) 
        {
            Console.WriteLine("value2 must be SByte");
            Console.Write("Exception Thrown: ");
            Console.Write("{0}", e.GetType(), e.Message);
        }
    }
}
 
 
Output:
  10 is greater than 9  

Example 2: For ArgumentException




// C# program to demonstrate the
// SByte.CompareTo(object) Method
using System;
using System.Globalization;
  
class GFG {
  
    // Main Method
    public static void Main()
    {
        try {
  
            // Declaring and initializing value1
            sbyte value1 = 10;
  
            // Declaring and initializing value2
            object value2 = 1 / 3;
  
            // using CompareTo() method
            int status = value1.CompareTo(value2);
  
            // checking the status
            if (status > 0)
                Console.WriteLine("{0} is greater than {1}",
                                            value1, value2);
  
            else if (status < 0)
                Console.WriteLine("{0} is less than {1}",
                                         value1, value2);
            else
                Console.WriteLine("{0} is equal to {1}",
                                        value1, value2);
        }
  
        catch (ArgumentException e) 
        {
            Console.WriteLine("value2 must be sbyte");
            Console.Write("Exception Thrown: ");
            Console.Write("{0}", e.GetType(), e.Message);
        }
    }
}
 
 
Output:
  value2 must be sbyte  Exception Thrown: System.ArgumentException  

Reference:

  • https://docs.microsoft.com/en-us/dotnet/api/system.sbyte.compareto?view=netcore-2.1


Next Article
UInt16.CompareTo() Method in C# with Examples

R

RohitPrasad3
Improve
Article Tags :
  • C#
  • CSharp-method
  • CSharp-SByte-Struct

Similar Reads

  • Single.CompareTo() Method in C# with Examples
    Single.CompareTo() Method is used to compare the current instance to a specified object or to another Single instance and returns an integer which shows whether the value of the current instance is greater than, equal to, or less than the value of the specified object or the other Single instance. T
    5 min read
  • UInt32.CompareTo() Method in C# with Examples
    UInt32.CompareTo Method is used to compare the current instance to a specified object or another UInt32 instance. It returns an integer which shows whether the value of the current instance is less than, equal to, or greater than the value of the specified object or the other UInt32 instance. There
    4 min read
  • UInt16.CompareTo() Method in C# with Examples
    UInt16.CompareTo Method is used to compare the current instance to a specified object or another UInt16 instance. It returns an integer which shows whether the value of the current instance is less than, equal to, or greater than the value of the specified object or the other UInt16 instance. There
    4 min read
  • UInt64.CompareTo() Method in C# with Examples
    UInt64.CompareTo Method is used to compare the current instance to a specified object or another UInt64 instance. It returns an integer which shows whether the value of the current instance is less than, equal to, or greater than the value of the specified object or the other UInt64 instance. There
    4 min read
  • Int32.CompareTo Method in C# with Examples
    Int32.CompareTo Method is used to compare the current instance to a specified object or Int32 and returns a sign of their relative values. There are 2 methods in the overload list of this method as follows: CompareTo(Int32) Method CompareTo(Object) Method Int32.CompareTo(Int32) Method This method is
    4 min read
  • Int64.CompareTo Method in C# with Examples
    Int64.CompareTo Method is used to compare the current instance to a specified object or Int64 and returns a sign of their relative values. There are 2 methods in the overload list of this method as follows: CompareTo(Int64) Method CompareTo(Object) Method Int64.CompareTo(Int64) Method This method is
    4 min read
  • Double.CompareTo Method in C# with Examples
    Double.CompareTo Method is used to compare the current instance to a specified object or Double object. It will return an integer which shows whether the value of the current instance is greater than, less than or equal to the value of the specified object or Double object. There are 2 methods in th
    5 min read
  • SByte.Equals Method in C# with Examples
    SByte.Equals Method is used to get a value which indicates whether the current instance is equal to a specified object or SByte or not. There are 2 methods in the overload list of this method as follows: Equals(SByte) Method Equals(Object) Method SByte.Equals(SByte) Method This method is used to ret
    3 min read
  • Single.Equals() Method in C# with Examples
    Single.Equals() Method is used to get a value which indicates whether the two instances of Single represents the same value or not. There are 2 methods in the overload list of this method as follows: Equals(Single) Method Equals(Object) Method Single.Equals(Single) Method This method is used to retu
    3 min read
  • SByte.ToString Method in C# with Examples | Set - 2
    SByte.ToString Method is used to convert the numeric value of the current instance to its equivalent string representation. There are 4 methods in the overload list of this method as follows: ToString(IFormatProvider) Method ToString(String, IFormatProvider) Method ToString() Method ToString(String)
    2 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences