Tuple<T1, T2, T3, T4> क्लास एक 4-टपल को दर्शाती है, जिसे चौगुना कहा जाता है। एक टपल एक डेटा संरचना है जिसमें तत्वों का क्रम होता है।
इसका उपयोग निम्न के लिए किया जाता है:
- डेटा सेट तक आसान पहुंच।
- डेटा सेट का आसान हेरफेर।
- डेटा के एकल सेट को दर्शाने के लिए।
- किसी विधि से एकाधिक मान लौटाने के लिए।
- किसी विधि को एकाधिक मान पास करने के लिए।
उदाहरण
आइए C# में 4-टपल को लागू करने का एक उदाहरण देखें:
using System;
public class Demo {
public static void Main(string[] args) {
Tuple<string,string,string,string> tuple = new Tuple<string,string,string,string>("nathan", "steve", "katie", "tim");
Console.WriteLine("मान (Item1)= " + tuple.Item1);
Console.WriteLine("मान (Item2)= " + tuple.Item2);
Console.WriteLine("मान (Item3)= " + tuple.Item3);
Console.WriteLine("मान (Item4)= " + tuple.Item4);
if (tuple.Item1 == "nathan") {
Console.WriteLine("मौजूद: टपल मान = " + tuple.Item1);
}
if (tuple.Item2 == "jack") {
Console.WriteLine("मौजूद: टपल मान = " + tuple.Item2);
}
if (tuple.Item3 == "katie") {
Console.WriteLine("मौजूद: टपल मान = " + tuple.Item3);
}
if (tuple.Item4 == "tom") {
Console.WriteLine("मौजूद: टपल मान = " + tuple.Item4);
}
}
}
आउटपुट
मान (Item1)= nathan मान (Item2)= steve मान (Item3)= katie मान (Item4)= tom मौजूद: टपल मान = nathan मौजूद: टपल मान = katie
उदाहरण (पूर्णांक टपल)
using System;
public class Demo {
public static void Main(string[] args) {
Tuple<int,int,int,int> tuple = new Tuple<int,int,int,int>(100, 150, 300, 450);
Console.WriteLine("मान (Item1)= " + tuple.Item1);
Console.WriteLine("मान (Item2)= " + tuple.Item2);
Console.WriteLine("मान (Item3)= " + tuple.Item3);
Console.WriteLine("मान (Item4)= " + tuple.Item4);
if (tuple.Item1 == 100) {
Console.WriteLine("मौजूद: टपल आइटम 1 = " + tuple.Item1);
}
if (tuple.Item2 == 250) {
Console.WriteLine("मौजूद: टपल आइटम 2 = " + tuple.Item2);
}
if (tuple.Item3 == 270) {
Console.WriteLine("मौजूद: टपल आइटम 3 = " + tuple.Item3);
}
if (tuple.Item4 == 300) {
Console.WriteLine("मौजूद: टपल आइटम 4 = " + tuple.Item4);
}
}
}
आउटपुट
मान (Item1)= 100 मान (Item2)= 150 मान (Item3)= 300 मान (Item4)= 450 मौजूद: टपल आइटम 1 = 100