주 메뉴 열기

wwiki β

바뀜

Lambda expressions

889 바이트 추가됨, 2021년 10월 28일 (목) 07:07
편집 요약 없음
Func<int, int> square = (x) => x*x;
Cosole.Write(square(10)); // 100출력
</syntaxhighlight><syntaxhighlight lang="c#">
// Declare a Func variable and assign a lambda expression to the
// variable. The method takes a string and converts it to uppercase.
Func<string, string> selector = str => str.ToUpper();
 
// Create an array of strings.
string[] words = { "orange", "apple", "Article", "elephant" };
// Query the array and select strings according to the selector method.
IEnumerable<String> aWords = words.Select(selector);
 
// Output the results to the console.
foreach (String word in aWords)
Console.WriteLine(word);
 
/*
This code example produces the following output:
 
ORANGE
APPLE
ARTICLE
ELEPHANT
 
*/
</syntaxhighlight>'Expression lambda'는 '[[Expression trees|expression tree]]' 타입으로 변환할 수 있다. <syntaxhighlight lang="csharp">
System.Linq.Expressions.Expression<Func<int, int>> e = x => x * x;
// Output:
// x => (x * x)
</syntaxhighlight><br />
== Expression lambdas(식 람다) ==
블록이 없는 람다<syntaxhighlight lang="c#">
(input-parameters) => expression
</syntaxhighlight>
== Statement lambdas(문 람다) ==
블록이 있는 람다이다.<syntaxhighlight lang="c#">
(input-parameters) => { <sequence-of-statements> }
</syntaxhighlight><br />
[[분류:.net]]
편집
2,431