지식 창고
람다식 & LINQ 본문
1. 람다식 ( Lamda expression )
class LamdaExpression
{
delegate void Lamda(); // 1. 대리자 선언
static void Main()
{
// 2. 대리자 개체에 람다식 정의
Lamda hi =
() => { Console.WriteLine("안녕하세요."); };
// 3. 대리자 개체 호출
hi();
}
}
class LamdaExpressionArgs
{
delegate int Lamda(int i); // 1. 대리자 선언
static void Main()
{
// 2. 대리자 개체에 람다식 정의
Lamda squre =
(x) => x * x;
console.WriteLine(squre(3));
}
}
class LamdaExpressionTypeDeclare
{
delegate bool Lamda(string, msg, int len);
static void Main()
{
Lamda isLong =
(msg, len) => { msg.Length > len; }
console.WriteLine(isLong("안녕하세요.", 5));
}
}
class LamdaExpressionWithFUnc
{
static void Main()
{
Func<int, int> squre = x => x*x;
console.WriteLine(squre(3));
}
}
2. LINQ ( Language INtegrated Query )
using System.Linq;
int[] numbers = { 1, 2, 3 };
numbers.Sum();
numbers.Max();
numbers.Where ( n => n%2 == 0 ).ToList(); > result : List<int>(2) {2, 4}
int[5] numbers = { 1, 2, 3, 4, 5 };
numbers.Select ( x => { return x; } );
numbers.Where ( n => n%2 == 0 || n%2 == 3 ).sum()
List<String> techs = new List<String>();techs.Add("C#");
techs.Add("ASP.NET");
techs.Add("Blazor");
techs.OrderBy(t => t); > result : List<string>(3) { "C#", "ASP.NET", "Blazor" }
techs.OrderByDescending(t => t.Length > 1);
var numbers = Enumerable.Range(1, 100);
numbers.Where ( n => n%2 == 0 ).Sum();
numbers.OrderByDescending ( n => n ).Where ( x => x%2 == 0 ).Take(3) > result : TakeIterator { 100, 98, 96 }
numbers.OrderByDescending ( n => n ).Where ( x => x%2 == 0 ).Skip(3).Take(3)
> result : TakeIterator { 94, 92, 90 }
var numbers = Enumerable.Range(1, 100);
var q = from n in numbers
where n%2 == 0
select n;
> result : {2, 4, 6, 8, 10, 12, ...... } 짝수만
'프로그래밍 > C#' 카테고리의 다른 글
데이터 형식 사용하기 (0) | 2021.11.02 |
---|---|
event Action (0) | 2021.11.02 |
Delegate (0) | 2021.11.02 |
Visual Studio C# Interactive (0) | 2021.11.02 |
[C#] 로그 라이브러리(log4net) (0) | 2021.11.02 |
Comments