|
Dieser Artikel wurde maschinell übersetzt. Bewegen Sie den Mauszeiger über die Sätze im Artikel, um den Originaltext anzuzeigen. Weitere Informationen
|
Übersetzung
Original
|
Operator => (C#-Referenz)
string[] words = { "cherry", "apple", "blueberry" }; // Use method syntax to apply a lambda expression to each element // of the words array. int shortestWordLength = words.Min(w => w.Length); Console.WriteLine(shortestWordLength); // Compare the following code that uses query syntax. // Get the lengths of each word in the words array. var query = from w in words select w.Length; // Apply the Min method to execute the query and get the shortest length. int shortestWordLength2 = query.Min(); Console.WriteLine(shortestWordLength2); // Output: // 5 // 5
int shortestWordLength = words.Min((string w) => w.Length);
static void Main(string[] args) { string[] digits = { "zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine" }; Console.WriteLine("Example that uses a lambda expression:"); var shortDigits = digits.Where((digit, index) => digit.Length < index); foreach (var sD in shortDigits) { Console.WriteLine(sD); } // Compare the following code, which arrives at the same list of short // digits but takes more work to get there. Console.WriteLine("\nExample that uses a for loop:"); List<string> shortDigits2 = new List<string>(); for (var i = 0; i < digits.Length; i++) { if (digits[i].Length < i) shortDigits2.Add(digits[i]); } foreach (var d in shortDigits2) { Console.WriteLine(d); } // Output: // Example that uses a lambda expression: // five // six // seven // eight // nine // Example that uses a for loop: // five // six // seven // eight // nine }