C# उन types को अलग करता है जो null हो सकते हैं उनसे जो नहीं हो सकते। Nullable value types (int?) value types को null रखने देते हैं, nullable reference types (एक compiler feature) null bugs को पकड़ने में मदद करते हैं, और null-handling operators (?., ??, !) संभावित-null values के साथ काम करना स्वच्छ व safe बनाते हैं — व्यापक null-reference समस्या को संबोधित करते हुए।
Nullable value types (int?)
int x = 5; // a value type — CANNOT be null
int? y = null; // int? = Nullable<int> — CAN be null
if (y.HasValue) { int val = y.Value; } // check + access
int z = y ?? 0; // default if null
Value types सामान्यतः null नहीं हो सकते, लेकिन int? (Nullable<int>) इसकी अनुमति देता है — optional/गायब data के लिए सामान्य (जैसे एक nullable database column)।
Nullable reference types (एक safety feature)
#nullable enable // (on by default in modern projects)
string name = null; // ⚠️ warning — string is non-nullable by default now
string? maybe = null; // ✅ string? explicitly allows null
void Use(string? s) {
Console.WriteLine(s.Length); // ⚠️ warning — s might be null (must check first)
}
nullable reference types enabled होने पर, compiler track करता है कि कौन से references null हो सकते हैं (string?) बनाम नहीं (string) और चेतावनी देता है जब आप null को dereference कर सकते हैं — NullReferenceException bugs को compile time पर पकड़ता है। यह एक प्रमुख modern C# safety feature है।
Null-handling operators
// ?. null-conditional — short-circuits to null instead of throwing
int? len = user?.Name?.Length; // null if user or Name is null (no exception)
user?.DoSomething(); // only calls if user isn't null
// ?? null-coalescing — provide a default
string name = input ?? "Anonymous";
name ??= "fallback"; // assign if currently null
// ! null-forgiving — assert non-null (suppress the warning; use carefully)
string definitelyNotNull = maybe!; // "I know it's not null" — no runtime check
null-conditional operator (?.) संभावित-null values के members को safely access करता है (throw करने के बजाय null लौटाता है), और ?? defaults प्रदान करता है — साथ में null handling को स्वच्छ व safe बनाते हुए।
यह क्यों महत्वपूर्ण है
Null handling robust C# लिखने के सबसे महत्वपूर्ण पहलुओं में से एक है, चूंकि NullReferenceException ("the billion-dollar mistake") सबसे सामान्य runtime errors में से एक है — और C# के nullable types व operators इसे सीधे संबोधित करते हैं, जिससे वे आवश्यक ज्ञान बन जाते हैं। Nullable value types (int?) optional/गायब data को represent करने के लिए व्यावहारिक रूप से आवश्यक हैं (nullable database fields, optional values)।
nullable reference types feature (अब modern .NET projects में default रूप से on) एक प्रमुख safety सुधार है: compiler से संभावित null dereferences के बारे में track व चेतावनी कराकर, यह null bugs के एक पूरे वर्ग को runtime crashes के बजाय compile time पर पकड़ता है — एक महत्वपूर्ण reliability लाभ जिसे आपको समझना और मानना चाहिए।
null-handling operators रोजमर्रा के tools हैं जो संभावित-null values के साथ काम करना स्वच्छ व safe बनाते हैं: null-conditional operator (?.) संभावित-null chains को safely navigate करता है (throw करने के बजाय null लौटाता है), ??/??= defaults को concisely प्रदान करते हैं, और ! non-null को assert करता है (सावधानी से उपयोग किया जाए)।
Nullable value types, nullable-reference-types safety feature, और null-handling operators (?., ??, !) को समझना null-safe, robust C# लिखने के लिए महत्वपूर्ण है जो व्यापक NullReferenceException से बचता है — एक बार-बार आने वाली, व्यावहारिक रूप से-महत्वपूर्ण चिंता।
चूंकि null-संबंधी bugs इतने सामान्य हैं और C# उन्हें रोकने के लिए मजबूत tools प्रदान करता है (विशेष रूप से compile-time null checking), nullable types व null operators में महारत हासिल करना reliable modern C# लिखने के लिए आधारभूत, अवश्य-जानने योग्य ज्ञान है।
