Ad space (banner)
#๏ธโƒฃC# Lessons
Lesson 47 / 68

Functions That Return Multiple Values (Tuples)

In this lesson you will learn how to return several values from one function in C# using tuples, so you are not limited to a single return value. This is for anyone searching for "C# tuple multiple return values".

A C# function can return several values at once by returning a tuple such as (int Min, int Max). Picture handing over one box containing two cards, a name and a score.

The sample code declares the return type as (int Min, int Max) MinMax(int[] nums) and returns the minimum and maximum together with return (nums.Min(), nums.Max()). The caller reaches each value by name, as result.Min and result.Max.

A common stumbling block for beginners is choosing between this and out parameters. Out parameters can return several values too, but tuples are considered the more modern and readable option.

In real-world development, being able to do casually with a tuple what other languages need an array or an object for is one of C#'s conveniences.

๐Ÿ“– Reference code
โœ๏ธ Your code
Type your code, then press "Run"

๐Ÿงช This site can't compile or run C# directly, so it checks on the spot whether what you typed matches the reference code (scoring happens entirely in your browser โ€” nothing is sent anywhere).

Ad space (banner)
Ad space (in-article)