← Back to C# Course | Chapter 15: Modern C# Features | Lesson 3 of 6

Tuples क्या हैं

एक tuple एक छोटा bundle है जो कई values साथ ले जाता है, जैसे एक lunch tray जिसमें sandwich और drink हो। यह किसी method को एक साथ एक से ज़्यादा चीज़ें return करने देता है।

In this page:

  1. Tuples
Syntax
csharp
(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

markup
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);
    }
}
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. #}
आम गलतियां
  1. tuple items को Item1 और Item2 के रूप में पढ़ना जब names ज़्यादा साफ़ होतीं।
  2. यह मान लेना कि tuples reference से compare होते हैं, जबकि वे value से compare होते हैं।
  3. tuple syntax में parentheses भूल जाना।
🔒

Chapter Quiz — Complete all 6 topics to unlock

0/6 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.