← Back to C# Course | Chapter 3: Operators | Lesson 2 of 6

Comparison Operators

In this page:

  1. Comparison Operators

Comparison Operators

Comparison operators (==, !=, >, <, >=, <=) compare two values and always produce a bool. For value types like int, == compares actual values. For reference types, == compares references unless the type overrides it.

Note: Use .Equals() or override == when you need value-based equality on custom classes.

Example: Comparison Operators

markup
using System;

class Program
{
    static void Main(string[] args)
    {
        int a = 5, b = 8;

        Console.WriteLine(a == b);
        Console.WriteLine(a != b);
        Console.WriteLine(a < b);
        Console.WriteLine(a >= 5);
    }
}
🔒

Chapter Quiz — Complete all 6 topics to unlock

0/6 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.