Tuples
In this page:
Tuples
ValueTuple lets a method return multiple values without declaring a dedicated class, written as (int, string) or with named elements. This is lighter weight than a full class when you just need to group a few related values temporarily. Tuples are value types, so they're compared and copied by value.
Note: Named tuple elements, like (int Count, string Label), make the returned values much more readable at the call site.
Example: 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
🔒
Chapter Quiz — Complete all 6 topics to unlock
0/6 topics done
Complete these topics first: