int[] numbers = { 1, 2, 2, 3, 3, 3, 4, 4, 4, 4 }; var distinctNumbers = numbers.Distinct(); foreach (var number in distinctNumbers) { Console.WriteLine(number); }
1 2 3 4
string[] names = { "John", "John", "Anna", "Mary", "Anna" }; var distinctNames = names.Distinct(); foreach (var name in distinctNames) { Console.WriteLine(name); }
John Anna MaryIn this example, we create an array of strings and call the Distinct method on it. The resulting sequence contains only the distinct names. The System Distinct method is part of the System.Linq namespace which is included in the System.Core assembly.