Tuples क्या हैं
एक tuple एक छोटा bundle है जो कई values साथ ले जाता है, जैसे एक lunch tray जिसमें sandwich और drink हो। यह किसी method को एक साथ एक से ज़्यादा चीज़ें return करने देता है।
In this page:
Syntax
(type, type) tuple = (value1, value2);
static (type Name1, type Name2) MethodName() { return (value1, value2); }
tuple.Item1;
var result = MethodName();
result.Name1;
Tuples
ValueTuple किसी method को एक dedicated class declare किए बिना कई values return करने देता है, (int, string) के रूप में या named elements के साथ लिखा गया।
जब आपको सिर्फ कुछ संबंधित values को अस्थायी रूप से group करना हो तो यह एक पूरी class से हल्का है। Tuples value types हैं, इसलिए इन्हें value से compare और copy किया जाता है।
Note:
Named tuple elements, जैसे
(int Count, string Label), call site पर returned values को कहीं ज़्यादा readable बनाते हैं।उदाहरण: Tuples
using System;
class Program
{
static (int Count, double Average) Analyze(int[] numbers)
{
int sum = 0;
foreach (int n in numbers) sum += n;
return (numbers.Length, (double)sum / numbers.Length);
}
static void Main(string[] args)
{
var result = Analyze(new int[] { 4, 8, 15 });
Console.WriteLine(result.Count);
Console.WriteLine(result.Average);
}
}
Login to try C/C++/Java/PHP code in the editor
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the
view already swaps in the hi_ translation fields (or blanks these
out if untranslated), so this renders correctly for both languages
without a lang_code check here. #}
आम गलतियां
- tuple items को
Item1औरItem2के रूप में पढ़ना जब names ज़्यादा साफ़ होतीं। - यह मान लेना कि tuples reference से compare होते हैं, जबकि वे value से compare होते हैं।
- tuple syntax में parentheses भूल जाना।
🔒
Chapter Quiz — Complete all 6 topics to unlock
0/6 topics done
Complete these topics first: