0Pricing
C# Academy · Lesson

Tuples as Method Return Values

Return multiple results without out parameters.

Tuples as Method Return Values is a free C# Academy lesson on CoddyKit — lesson 4 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the C# Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

Returning Multiple Values

A method can return only one thing, but a tuple lets that one thing bundle several values, a clean alternative to out parameters.

using System;

class Program
{
    static (int, int) Divide(int a, int b) => (a / b, a % b);

    static void Main()
    {
        var result = Divide(17, 5);
        Console.WriteLine(result.Item1 + " r " + result.Item2);
    }
}

Named Tuple Return Types

Naming the returned elements makes the method self-documenting and the call site readable.

using System;

class Program
{
    static (int Quotient, int Remainder) Divide(int a, int b)
    {
        return (a / b, a % b);
    }

    static void Main()
    {
        var r = Divide(17, 5);
        Console.WriteLine("Q=" + r.Quotient + ", R=" + r.Remainder);
    }
}

Deconstructing the Return

Callers can deconstruct the returned tuple directly into separate variables.

using System;

class Program
{
    static (double Min, double Max) Bounds(double[] data)
    {
        double min = data[0], max = data[0];
        foreach (var d in data)
        {
            if (d < min) min = d;
            if (d > max) max = d;
        }
        return (min, max);
    }

    static void Main()
    {
        var (lo, hi) = Bounds(new double[] { 3, 7, 1, 9, 4 });
        Console.WriteLine("min=" + lo + ", max=" + hi);
    }
}

The out Parameter Alternative

Before tuples, returning extras meant out parameters. They work, but are clunkier to declare and call.

using System;

class Program
{
    static int Divide(int a, int b, out int remainder)
    {
        remainder = a % b;
        return a / b;
    }

    static void Main()
    {
        int rem;
        int q = Divide(17, 5, out rem);
        Console.WriteLine("Q=" + q + ", R=" + rem);
    }
}

Tuples vs out: Readability

Tuple returns keep all results in the return value and read naturally with deconstruction, so they are usually preferred over out for simple cases.

using System;

class Program
{
    static (int Sum, int Count) Stats(int[] nums)
    {
        int sum = 0;
        foreach (var n in nums) sum += n;
        return (sum, nums.Length);
    }

    static void Main()
    {
        var (sum, count) = Stats(new[] { 2, 4, 6 });
        Console.WriteLine("Sum=" + sum + ", Count=" + count);
    }
}

Returning a Status and a Value

A common pattern is returning a success flag alongside the result, similar to the Try-pattern but bundled in one tuple.

using System;

class Program
{
    static (bool Ok, int Value) ParseAge(string s)
    {
        if (int.TryParse(s, out int n) && n >= 0)
            return (true, n);
        return (false, 0);
    }

    static void Main()
    {
        var r = ParseAge("42");
        Console.WriteLine(r.Ok ? "Parsed " + r.Value : "Invalid");
    }
}

When out Is Still Useful

out remains idiomatic for the built-in Try-pattern (int.TryParse), where one boolean result and one out value are conventional.

using System;

class Program
{
    static void Main()
    {
        if (int.TryParse("100", out int value))
            Console.WriteLine("Got " + value);
        else
            Console.WriteLine("Not a number");
    }
}

Returning Several Named Fields

Tuples scale to several results without forcing you to create a one-off class.

using System;

class Program
{
    static (int Min, int Max, double Avg) Analyze(int[] nums)
    {
        int min = nums[0], max = nums[0], sum = 0;
        foreach (var n in nums)
        {
            if (n < min) min = n;
            if (n > max) max = n;
            sum += n;
        }
        return (min, max, (double)sum / nums.Length);
    }

    static void Main()
    {
        var a = Analyze(new[] { 4, 8, 2, 6 });
        Console.WriteLine("min=" + a.Min + ", max=" + a.Max + ", avg=" + a.Avg);
    }
}

When to Prefer a Class

If the grouped data has behavior, is reused widely, or needs validation, define a class or record instead of returning a tuple everywhere.

using System;

class Program
{
    // Fine as a tuple: small, local, no behavior
    static (int W, int H) ParseSize(string s)
    {
        var parts = s.Split('x');
        return (int.Parse(parts[0]), int.Parse(parts[1]));
    }

    static void Main()
    {
        var (w, h) = ParseSize("16x9");
        Console.WriteLine(w + " by " + h);
    }
}

Chaining Returned Tuples

You can feed a returned tuple straight into another computation by deconstructing it inline.

using System;

class Program
{
    static (int, int) Split(int total) => (total / 2, total - total / 2);

    static void Main()
    {
        var (a, b) = Split(7);
        Console.WriteLine("Halves: " + a + " + " + b + " = " + (a + b));
    }
}

Putting It Together

Returning a named tuple is the modern, readable way to hand back a few related values from a method without extra ceremony.

using System;

class Program
{
    static (bool Found, int Index) Search(int[] arr, int target)
    {
        for (int i = 0; i < arr.Length; i++)
            if (arr[i] == target) return (true, i);
        return (false, -1);
    }

    static void Main()
    {
        var r = Search(new[] { 5, 8, 12 }, 8);
        Console.WriteLine(r.Found ? "At index " + r.Index : "Not found");
    }
}

Quick Check

Test your understanding of tuple return values.

Recap

Returning a tuple lets a method hand back several values at once, a cleaner option than out for most cases. Name the elements for clarity and let callers deconstruct them. Keep out for the conventional Try-pattern, and prefer a class or record when the data has behavior or wide reuse.

using System;

class Program
{
    static (int Sum, int Max) Go(int[] n)
    {
        int s = 0, m = n[0];
        foreach (var x in n) { s += x; if (x > m) m = x; }
        return (s, m);
    }

    static void Main()
    {
        var (sum, max) = Go(new[] { 3, 9, 5 });
        Console.WriteLine(sum + " / " + max);
    }
}

Frequently asked questions

Is the “Tuples as Method Return Values” lesson free?

Yes — the full text of “Tuples as Method Return Values” is free to read here on the web, and the C# Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the C# Academy course, upgrade to CoddyKit PRO.

What will I learn in “Tuples as Method Return Values”?

Return multiple results without out parameters. You practise C# Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start C# Academy?

No prior experience is required. C# Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 4 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Tuples as Method Return Values” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this C# Academy lesson?

Yes. Every C# Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Value Tuples Basics
  2. Named Tuple Elements
  3. Deconstruction
  4. Tuples as Method Return Values
← Back to C# Academy