C# विशेष डेटा प्रकार प्रदान करता है, नल योग्य प्रकार, जिनको आप सामान्य मानों की सीमा के साथ-साथ नल मान भी असाइन कर सकते हैं।
C# 2.0 ने नल योग्य प्रकार पेश किए जो आपको वैल्यू टाइप वेरिएबल्स को नल असाइन करने की अनुमति देते हैं। आप Nullable<T> का उपयोग करके नल योग्य प्रकार घोषित कर सकते हैं जहां T एक प्रकार है (शॉर्टहैंड: T?)।
नल योग्य प्रकार के मुख्य गुण
- नल योग्य प्रकार केवल वैल्यू प्रकारों के साथ उपयोग किए जा सकते हैं
Valueप्रॉपर्टीInvalidOperationExceptionफेंकती है यदि मान नल है; अन्यथा मान लौटाती हैHasValueप्रॉपर्टीtrueलौटाती है यदि वेरिएबल में मान है, याfalseयदि यह नल है- आप नल योग्य प्रकार के साथ केवल
==और!=ऑपरेटर्स का उपयोग कर सकते हैं - नेस्टेड नल योग्य प्रकार अनुमत नहीं हैं:
Nullable<Nullable<int>>कंपाइल टाइम एरर देगा
उदाहरण 1: एक्सटेंशन मेथड के साथ पार्सिंग
static class Program
{
static void Main(string[] args)
{
string s = "123";
Console.WriteLine(s.ToNullableInt()); // आउटपुट: 123
string nullStr = null;
Console.WriteLine(nullStr.ToNullableInt()); // आउटपुट: (खाली - नल)
string invalid = "abc";
Console.WriteLine(invalid.ToNullableInt()); // आउटपुट: (खाली - नल)
Console.ReadLine();
}
// एक्सटेंशन मेथड
static int? ToNullableInt(this string s)
{
if (int.TryParse(s, out int i))
return i;
return null;
}
}
आउटपुट
123
(खाली लाइन - नल मान)
बिल्ट-इन तरीके (.NET Core 2.0+)
// 1. int.TryParse के साथ पैटर्न मिलान (C# 7+)
public static int? ParseNullable(string s)
{
return int.TryParse(s, out int result) ? result : (int?)null;
}
// 2. नल-सहनीय रेफरेंस प्रकारों के साथ (C# 8+)
public static int? TryParse(string? input)
{
return int.TryParse(input, out int result) ? result : null;
}
// 3. Convert.ToInt32 (नल के लिए 0 लौटाता है, नल नहीं!)
int? result = string.IsNullOrEmpty(s) ? null : Convert.ToInt32(s);
उपयोग पैटर्न
string input = "42";
// पैटर्न 1: HasValue चेक
int? parsed = input.ToNullableInt();
if (parsed.HasValue)
{
Console.WriteLine($"Parsed: {parsed.Value}");
}
// पैटर्न 2: नल-कोएलिसिंग ऑपरेटर
int value = parsed ?? -1; // डिफ़ॉल्ट -1 यदि नल
// पैटर्न 3: पैटर्न मिलान (C# 7+)
if (parsed is int val)
{
Console.WriteLine($"Value: {val}");
}
// पैटर्न 4: स्विच एक्सप्रेशन (C# 8+)
string status = parsed switch
{
null => "Not parsed",
> 100 => "Large",
> 0 => "Positive",
_ => "Zero or negative"
};
सामान्य परिदृश्य: उपयोगकर्ता इनपुट पार्सिंग
public static class StringExtensions
{
public static int? ToNullableInt(this string? s)
{
if (string.IsNullOrWhiteSpace(s)) return null;
return int.TryParse(s.Trim(), out int result) ? result : null;
}
public static double? ToNullableDouble(this string? s)
{
if (string.IsNullOrWhiteSpace(s)) return null;
return double.TryParse(s.Trim(), out double result) ? result : null;
}
public static DateTime? ToNullableDateTime(this string? s)
{
if (string.IsNullOrWhiteSpace(s)) return null;
return DateTime.TryParse(s.Trim(), out DateTime result) ? result : null;
}
}
// उपयोग
string userInput = Console.ReadLine();
int? age = userInput.ToNullableInt();
double? price = userInput.ToNullableDouble();
DateTime? dob = userInput.ToNullableDateTime();
सावधान: Convert.ToInt32 बनाम int.TryParse
| इनपुट | int.TryParse | Convert.ToInt32 |
|---|---|---|
| "123" | 123 | 123 |
| "" | नल (विफल) | 0 |
| null | नल (विफल) | 0 |
| "abc" | नल (विफल) | FormatException |
नल योग्य int के लिए हमेशा int.TryParse का उपयोग करें - Convert.ToInt32 नल/खाली के लिए 0 लौटाता है जो गलत सकारात्मक हो सकता है।