delegate : 메서드를 변수로 사용하는 개념 (C 함수포인트와 비슷함)
대표적으로 Callback method 에서 사용
+- 연산자 사용 가능
예) List.Sort(delegate) 의 매개변수로 비교함수 Comparison 을 사용
무명 델리게이트 : 인라인 함수처럼 사용
delegate 와 비슷한 기능을 하며 좀 더 짧게 사용 가능
// 기본 델리게이트
products.Sort(SortWithPrice);
private static int SortWithPrice(Product x, Product y)
{
return x.Price.CompareTo(y.Price);
}
// 무명 델리게이트
products.Sort(delegate (Product x, Product y)
{
return x.Price.CompareTo(y.Price);
});
// 람다
products.Sort((a, b) => a.Price.CompareTo(b.Price));
// 람다 2
products.Sort((a, b) =>
{
return a.Price.CompareTo(b.Price);
});
참조)
out, in ( Call by Reference )
out : 반드시 변경
in : 변경하면 안됨
참조)
Closer 변수
지역변수를 선언하고 범위를 벗어나더라도
함수가 지속적으로 살아있는 경우 지역변수를 살려둠
delegate 사용 시 보임