CSharp examples for Language Basics:Tuple
Declare tuple type and return it
using System;/*from w ww . j av a 2s . co m*/ using System.Collections.Generic; using System.Linq; class MinMax2 { static void Main() { Random rng = new Random(); var numbers = Enumerable.Range(0, 100).Select(_ => rng.Next(1000)); var minMax = MinMax(numbers); Console.WriteLine($"Minimum: {minMax.min}"); Console.WriteLine($"Maximum: {minMax.max}"); } static (int min, int max) MinMax(IEnumerable<int> source) { using (var iterator = source.GetEnumerator()) { if (!iterator.MoveNext()) { throw new InvalidOperationException("Cannot find min/max of an empty sequence"); } var result = (min: iterator.Current, max: iterator.Current); while (iterator.MoveNext()) { result.min = Math.Min(result.min, iterator.Current); result.max = Math.Max(result.max, iterator.Current); } return result; } } }