/
aprogrammer
/
dotnet-docs
Обзор
Документация
Войти
/
aprogrammer
/
dotnet-docs
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
Аналитика
Безопасность
main
docs/csharp/snippets/methods/params75.cs
39 строк
1 KB
Bill Wagner
Add params collections (#40978)
21 май 2024, 00:01
Не верифицирован
21 май 2024, 00:01
57cbe8e
Код
Авторство
О чём код?
//<Snippet75> static class ParamsExample { static void Main() { string fromArray = GetVowels(["apple", "banana", "pear"]); Console.WriteLine($"Vowels from collection expression: '{fromArray}'"); string fromMultipleArguments = GetVowels("apple", "banana", "pear"); Console.WriteLine($"Vowels from multiple arguments: '{fromMultipleArguments}'"); string fromNull = GetVowels(null); Console.WriteLine($"Vowels from null: '{fromNull}'"); string fromNoValue = GetVowels(); Console.WriteLine($"Vowels from no value: '{fromNoValue}'"); } static string GetVowels(params IEnumerable<string>? input) { if (input == null || !input.Any()) { return string.Empty; } char[] vowels = ['A', 'E', 'I', 'O', 'U']; return string.Concat( input.SelectMany( word => word.Where(letter => vowels.Contains(char.ToUpper(letter))))); } } // The example displays the following output: // Vowels from array: 'aeaaaea' // Vowels from multiple arguments: 'aeaaaea' // Vowels from null: '' // Vowels from no value: '' //</Snippet75>