How to return multiple values in C 7?
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
C# 7 made returning multiple values much cleaner by adding tuple syntax to the language. Instead of defining a throwaway class or relying on several out parameters, you can return a small group of related values directly from a method.
Return a Tuple
The core syntax is simple: declare the method return type as a tuple and return matching values.
The names sum and product are optional but strongly recommended. Named tuple members make the result easier to read and prevent code from turning into a series of Item1, Item2, and Item3 accesses.
Deconstruct the Return Value
If the caller wants the values as separate local variables, deconstruction is even clearer.
This style is compact and expressive when the returned values belong together conceptually but do not justify a separate type.
Compare Tuples with Older Approaches
Before C# 7, the most common alternatives were out parameters and custom classes or structs.
out parameters still work:
This is appropriate for TryParse style methods where success and output are tightly linked. For general "return several pieces of data" cases, tuples are easier to read.
A custom type is better when the returned data has behavior, validation rules, or long-term meaning in your domain. Tuples are best for lightweight groupings, not for every public API.
When a Custom Type Is Better
If the method returns many fields, or the result will be passed around widely, a named class, record, or struct usually communicates intent better than a tuple.
For example, this:
can be better than returning a long tuple because the result has a stable identity and self-documenting meaning.
A practical rule is:
- use tuples for short-lived helper results
- use
outparameters forTryXxxpatterns - use a custom type when the result is part of the domain model or public contract
Performance and Readability
Value tuples are lightweight and generally efficient for typical application code. In practice, readability matters more than tiny performance differences here. The bigger mistake is choosing a form that hides meaning from callers.
If consumers keep forgetting what Item1 and Item2 represent, the code is telling you to add names or introduce a real type.
Common Pitfalls
- Returning unnamed tuples and forcing callers to use
Item1andItem2, which hurts readability. - Replacing every small type with tuples. Some results deserve a named type because they carry domain meaning.
- Using tuples where a
TryXxxmethod withoutparameters is the established .NET pattern. - Returning too many tuple elements. Large tuples quickly become harder to understand than a small class or record.
- Assuming tuple field names are a full substitute for good method names and documentation.
Summary
- In C# 7, tuples are the usual way to return multiple values from a method.
- Named tuple members make results much easier to consume than
Item1style access. - Deconstruction lets callers unpack tuple values into separate variables cleanly.
- '
outparameters still fitTryXxxpatterns well.' - Use a custom type when the returned data has lasting semantic meaning.

