DevAcademia
C++C#CPythonJava
  • C# Basics

  • C# Introduction
  • C# Get Started
  • C# Syntax
  • C# Output
  • C# Comments
  • C# Variables
  • C# Data Types
  • C# Type Casting
  • C# User Input
  • C# Operators
  • C# Math
  • C# Strings
  • C# Booleans
  • C# If...Else
  • C# Switch Statement
  • C# While Loop
  • C# For Loop
  • C# Break and Continue
  • C# Arrays
  • C# Files
  • C# OOP

  • C# OOP Introduction
  • C# Classes and Objects
  • C# Class Members
  • C# Constructors
  • C# Destructors
  • C# Access Modifiers
  • C# Properties
  • C# Inheritance
  • C# Polymorphism
  • C# Abstraction
  • C# Interfaces
  • C# Enums
  • C# Exceptions
  • C# Quizzes

  • C# Quiz Introduction
  • C# Basics

  • C# Introduction
  • C# Get Started
  • C# Syntax
  • C# Output
  • C# Comments
  • C# Variables
  • C# Data Types
  • C# Type Casting
  • C# User Input
  • C# Operators
  • C# Math
  • C# Strings
  • C# Booleans
  • C# If...Else
  • C# Switch Statement
  • C# While Loop
  • C# For Loop
  • C# Break and Continue
  • C# Arrays
  • C# Files
  • C# OOP

  • C# OOP Introduction
  • C# Classes and Objects
  • C# Class Members
  • C# Constructors
  • C# Destructors
  • C# Access Modifiers
  • C# Properties
  • C# Inheritance
  • C# Polymorphism
  • C# Abstraction
  • C# Interfaces
  • C# Enums
  • C# Exceptions
  • C# Quizzes

  • C# Quiz Introduction

Loading Cs tutorial…

Loading content
C# BasicsTopic 16 of 55
←PreviousPrevNextNext→

C# Comparison Operators - Complete Guide

Introduction to Comparison Operators

Comparison operators in C# are used to compare two values and return a boolean result (true or false). These operators are essential for making decisions in your code through conditional statements like if, while, and for loops.

C# provides a comprehensive set of comparison operators that work with various data types, including numeric types, strings, and objects. Understanding how to use these operators correctly is crucial for implementing logic in your programs.

Basic Comparison Operators

C# includes the standard comparison operators for equality and relational comparisons:

Example
using System;

namespace ComparisonOperatorsExample
{
    class Program
    {
        static void Main(string[] args)
        {
            int a = 10;
            int b = 5;
            int c = 10;
            
            // Equality operators
            Console.WriteLine($"{a} == {c}: {a == c}");  // Equal to
            Console.WriteLine($"{a} != {b}: {a != b}");  // Not equal to
            
            // Relational operators
            Console.WriteLine($"{a} > {b}: {a > b}");    // Greater than
            Console.WriteLine($"{a} < {b}: {a < b}");    // Less than
            Console.WriteLine($"{a} >= {c}: {a >= c}");  // Greater than or equal to
            Console.WriteLine($"{b} <= {a}: {b <= a}");  // Less than or equal to
            
            // Floating-point comparisons
            double x = 0.1 + 0.2;
            double y = 0.3;
            
            Console.WriteLine($"\nFloating-point comparison:");
            Console.WriteLine($"0.1 + 0.2 = {x}");
            Console.WriteLine($"0.3 = {y}");
            Console.WriteLine($"x == y: {x == y}"); // False due to floating-point precision
            Console.WriteLine($"Math.Abs(x - y) < 0.0001: {Math.Abs(x - y) < 0.0001}"); // True
            
            // Char comparisons (based on Unicode values)
            char char1 = 'A';
            char char2 = 'B';
            char char3 = 'a';
            
            Console.WriteLine($"\nChar comparisons:");
            Console.WriteLine($"'A' < 'B': {char1 < char2}"); // True (65 < 66)
            Console.WriteLine($"'A' == 'a': {char1 == char3}"); // False (65 != 97)
            Console.WriteLine($"'A' < 'a': {char1 < char3}"); // True (65 < 97)
        }
    }
}
Output
10 == 10: True
10 != 5: True
10 > 5: True
10 < 5: False
10 >= 10: True
5 <= 10: True

Floating-point comparison:
0.1 + 0.2 = 0.30000000000000004
0.3 = 0.3
x == y: False
Math.Abs(x - y) < 0.0001: True

Char comparisons:
'A' < 'B': True
'A' == 'a': False
'A' < 'a': True

String Comparison

String comparison in C# requires special attention due to reference vs value semantics:

Example
using System;

namespace StringComparisonExample
{
    class Program
    {
        static void Main(string[] args)
        {
            string str1 = "hello";
            string str2 = "hello";
            string str3 = "HELLO";
            string str4 = new string(new char[] { 'h', 'e', 'l', 'l', 'o' });
            
            // Equality comparison (== compares string contents)
            Console.WriteLine($"str1 == str2: {str1 == str2}"); // True
            Console.WriteLine($"str1 == str3: {str1 == str3}"); // False (case different)
            Console.WriteLine($"str1 == str4: {str1 == str4}"); // True (same content)
            
            // Reference equality
            Console.WriteLine($"ReferenceEquals(str1, str2): {object.ReferenceEquals(str1, str2)}"); // True (interned)
            Console.WriteLine($"ReferenceEquals(str1, str4): {object.ReferenceEquals(str1, str4)}"); // False
            
            // String.Equals with options
            Console.WriteLine($"str1.Equals(str3): {str1.Equals(str3)}"); // False
            Console.WriteLine($"str1.Equals(str3, StringComparison.OrdinalIgnoreCase): {str1.Equals(str3, StringComparison.OrdinalIgnoreCase)}"); // True
            
            // CompareTo method
            Console.WriteLine($"'apple'.CompareTo('banana'): {"apple".CompareTo("banana")}"); // -1
            Console.WriteLine($"'banana'.CompareTo('apple'): {"banana".CompareTo("apple")}"); // 1
            Console.WriteLine($"'apple'.CompareTo('apple'): {"apple".CompareTo("apple")}"); // 0
        }
    }
}
Output
str1 == str2: True
str1 == str3: False
str1 == str4: True
ReferenceEquals(str1, str2): True
ReferenceEquals(str1, str4): False
str1.Equals(str3): False
str1.Equals(str3, StringComparison.OrdinalIgnoreCase): True
'apple'.CompareTo('banana'): -1
'banana'.CompareTo('apple'): 1
'apple'.CompareTo('apple'): 0
Test your knowledge: C# Comparison Operators - Complete Guide
Quiz Configuration
4 of 8 questions
Sequential
Previous allowed
Review enabled
Early close allowed
Estimated time: 5 min
C# BasicsTopic 16 of 55
←PreviousPrevNextNext→