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

Comparison Operators कैसे काम करते हैं

Comparison operators 'क्या यह बड़ा है?' या 'क्या ये बराबर हैं?' जैसे सवाल पूछते हैं और true या false से जवाब देते हैं। वे दो चीज़ों को तौलने वाले एक scale जैसे हैं।

In this page:

  1. Comparison Operators
Syntax
csharp
bool result = a == b;
bool result = a != b;
bool result = a > b;
bool result = a <= b;

Comparison Operators

Comparison operators (==, !=, >, <, >=, <=) दो values compare करते हैं और हमेशा एक bool produce करते हैं। int जैसे value types के लिए, == असली values compare करता है। Reference types के लिए, == references compare करता है जब तक type इसे override न करे।

Note: custom classes पर value-based equality चाहिए हो तो .Equals() का उपयोग करें या == को override करें।

उदाहरण: 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);
    }
}
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
आम गलतियां
  1. if (a == b) के बजाय if (a = b) लिखना; = एक value assign करता है, और int values के लिए assignment एक bool नहीं है, इसलिए code compile नहीं होता।
  2. floating-point results को == से compare करना, जैसे 0.1 + 0.2 == 0.3, जो छोटी rounding errors की वजह से false है; इसके बजाय एक छोटी tolerance के भीतर compare करें।
  3. यह उम्मीद करना कि == हर reference type के लिए contents compare करेगा, जबकि बिना overloaded == वाली classes के लिए यह सिर्फ जांचता है कि दोनों variables एक ही object को point करते हैं या नहीं (string exception है और value से compare करता है)।
🔒

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.