टपल को अनइनिशियलाइज़्ड/नल छोड़ना
टपल को खाली/नल छोड़ने के लिए बस डिक्लेयर करें बिना इनिशियलाइज़ किए:
Tuple<string, string> myTuple;
यह डिफ़ॉल्ट रूप से null होगा (रेफरेंस टाइप होने के कारण)।
टपल में नल चेक करना:
using System;
namespace Demo {
class Program {
static void Main(string[] args) {
Tuple<int, string> tuple = new Tuple<int, string>(10, null);
if (tuple.Item1 == 10) {
Console.WriteLine(tuple.Item1);
}
if (tuple.Item2 == null) {
Console.WriteLine("Item is null");
}
}
}
}
आउटपुट:
10 Item is null
महत्वपूर्ण बिंदु:
Tuple<T1, T2>एक रेफरेंस टाइप है - डिफ़ॉल्ट nullValueTuple<T1, T2>(C# 7+) एक वैल्यू टाइप है - डिफ़ॉल्ट (0, null) आदि- टपल इम्म्यूटेबल हैं - बनने के बाद मान नहीं बदल सकते
आधुनिक ValueTuple सिंटैक्स:
// नल टपल (string, int)? nullableTuple = null; // डिफ़ॉल्ट वैल्यू टपल var tuple = (Name: default(string), Age: default(int)); // tuple.Name == null, tuple.Age == 0 // या सीधे (string Name, int Age) person = (null, 0);