BCA / B.Tech 8 min read

Nullable Types in .NET in Hindi

Nullable Types in .NET in Hindi |  .NET में Nullable Types हिंदी में :


  • .NET में Nullable types का उपयोग उन डेटा प्रकारों (data types) के लिए किया जाता है, जिन्हें null मान स्वीकार करने की आवश्यकता होती है।
  •  जैसे कि, int, double, और bool जैसे Value Types (मान प्रकार) डिफ़ॉल्ट रूप से null नहीं हो सकते, लेकिन Nullable types के जरिए आप इन्हें null मान दे सकते हैं।
  • Nullable types .NET में उन स्थितियों के लिए बहुत उपयोगी हैं, जब Value Types को null मान चाहिए। यह कोड को सुरक्षित, पठनीय और आसान बनाते हैं।
Nullable Types क्या हैं?

  • Nullable type एक ऐसा डेटा प्रकार है जो किसी मान (value) के साथ-साथ null भी धारण कर सकता है। इसे .NET में System.Nullable<T> के रूप में परिभाषित किया गया है।
उदाहरण: यदि आप किसी डेटाबेस कॉलम का डेटा स्टोर कर रहे हैं और वह कॉलम खाली हो सकता है, तो Nullable type का उपयोग करेंगे।

int? age = null; // यह Nullable int है, जो null मान स्वीकार कर सकता है।

  • Nullable types केवल Value Types (जैसे int, double) के लिए उपयोगी हैं। Reference Types (जैसे string) पहले से ही null हो सकते हैं।
  • Value प्रॉपर्टी का उपयोग केवल तब करें, जब HasValue true हो, अन्यथा यह Exception देगा।

उदाहरण:

using System;

class Program
{
    static void Main()
    {
        int? age = null; // Nullable type
        Console.WriteLine("Age: " + (age.HasValue ? age.Value.ToString() : "No value"));

        age = 30;
        Console.WriteLine("Age: " + age ?? "No value"); // Null Coalescing Operator
    }
}


Nullable Types कैसे डिक्लेयर करें?

Question Mark (?) का उपयोग: किसी Value Type के बाद ? लगाकर उसे Nullable बना सकते हैं।

int? age = null;
double? salary = 50000.50;
System.Nullable<T> का उपयोग:

Generic प्रकार का उपयोग करके भी Nullable types बना सकते हैं।

Nullable<int> age = null;
Nullable<double> salary = 100000.75;

Nullable Types का उपयोग क्यों करें?

  • Null मान स्टोर करना: उन स्थितियों में उपयोगी, जब डेटा अनुपलब्ध हो।
  • डेटाबेस के साथ काम करना: डेटाबेस कॉलम जो खाली (null) हो सकते हैं, उनके लिए।
  • कंडीशनल प्रोग्रामिंग: लॉजिकल निर्णय लेने में मदद करता है, जैसे किसी वैल्यू के मौजूद होने या न होने पर।

Nullable Types के साथ कार्य करना :

1. HasValue और Value प्रॉपर्टी: HasValue: यह चेक करता है कि Nullable type में वैल्यू है या नहीं।
Value: इसका उपयोग वैल्यू प्राप्त करने के लिए किया जाता है।

int? age = 25;
if (age.HasValue)
{
    Console.WriteLine("Age: " + age.Value);
}
else
{
    Console.WriteLine("Age is null");
}

2. Null Coalescing Operator (??): यह ऑपरेटर डिफ़ॉल्ट वैल्यू सेट करने में मदद करता है।

int? age = null;
int defaultAge = age ?? 18; // अगर age null है, तो defaultAge 18 होगा।
Console.WriteLine(defaultAge);

3. Null-Conditional Operator (?.): यह ऑपरेटर किसी वैल्यू के null होने पर सुरक्षित रूप से चेक करता है।

int? age = null;
Console.WriteLine(age?.ToString() ?? "No age available");
Nullable Types के साथ Cast और Boxing

Boxing: जब Nullable type को Object में कन्वर्ट करते हैं।

int? num = 10;
object obj = num; // Boxing
Console.WriteLine(obj);

Unboxing: Object से Nullable type में कन्वर्ट करते हैं।

object obj = null;
int? num = (int?)obj; // Unboxing
Console.WriteLine(num);

In this Chapter

Nullable Types in .NET in Hindi
Introduction of .Net in Hindi
Web Services in Hindi | वेब सर्विसेस हिंदी में
WSDL in Hindi | WSDL हिंदी में
Boxing & Unboxing in ADO.NET in Hindi
CLR in Hindi | CLR क्या है?
Common Types System in Hindi
MSIL in Hindi
Assemblies & Class Libraries in Hindi
Project of .Net in Hindi
What is VB.NET and IDE in Hindi | वीबी.नेट क्या है ?
Intermediate Language in Hindi
Object Orientation in Hindi
Managed Execution in Hindi
Rapid Development in Hindi
Windows Presentation Foundation in Hindi
Whats new For .NET framework 3.5?
Windows Workflow Foundation (WWF) in Hindi
Windows Card Space in Hindi
Windows Communication Foundation in Hindi
How To Install and Use The Visual Studio 2008
How to Working With Visual Studio 2008
Types of Visual Studio 2008 in Hindi
Visual Studio 2008 IDE in Hindi
How To Create Console Application in Hindi
Introduction of C# in .NET in Hindi
Classes of .NET With C# in Hindi
Properties of .NET With C# in Hindi
Structs in C# .NET in Hindi
Delegates & Events in Hindi
Generic Collections in .NET (C#) in Hindi
Type Safety in Hindi
ADO.NET in Hindi | ADO.NET क्या है?
SQL Connection Object in Hindi
SQL Command in Hindi
LINQ in Hindi | LINQ क्या है?
What is Using Stored Procedures?
Windows Application in .NET in Hindi | Windows Application क्या है?
BCA || .NET with C# 2023 Paper | MDSU Exam Paper
.NET with C# All Important Questions and Answers in Hindi (MDSU)
BCA || .NET with C# 2025 Paper | MDSU Exam Paper