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

Null Coalescing (??) की जानकारी

Double question mark आपको एक backup value देता है जब कुछ खाली होता है, जैसे 'मेरा lunchbox उपयोग करो, या अगर मैं भूल गया तो lunch खरीद लो'। यह जब भी पहली चीज़ null हो एक default प्रदान करता है।

In this page:

  1. Null Coalescing (??)
Syntax
csharp
type result = value ?? fallback;   // fallback used when value is null
variable ??= fallback;              // assign only if variable is null

Null Coalescing (??)

?? operator अगर यह null न हो तो अपना left operand return करता है, नहीं तो यह right operand return करता है — defaults देने का एक compact तरीका।

??= आगे जाता है, सिर्फ तभी right side assign करते हुए जब variable वर्तमान में null हो। दोनों operators specifically verbose null-checking if statements घटाने के लिए मौजूद हैं।

Note: x ??= defaultValue;, if (x == null) x = defaultValue; का shorthand है।

उदाहरण: Null Coalescing (??)

markup
using System;

class Program
{
    static void Main(string[] args)
    {
        string name = null;
        string displayName = name ?? "Guest";

        Console.WriteLine(displayName);

        string city = null;
        city ??= "Unknown";
        Console.WriteLine(city);
    }
}
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. किसी value type पर ?? का उपयोग करना जो null नहीं हो सकता, जो एक compile error है।
  2. ?? (null पर default) को ?: (एक bool पर conditional) के साथ confuse करना।
  3. यह उम्मीद करना कि ?? किसी खाली string को null मानेगा, जबकि यह सिर्फ null जांचता है।
चैप्टर सारांश
  • Arithmetic, comparison, और logical operators expressions बनाते हैं।
  • Bitwise operators values के bits पर काम करते हैं।
  • Ternary operator और null coalescing (??) values चुनने के छोटे तरीके प्रदान करते हैं।
🔒

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.