람다 식으로 정의가 가능한 유형은 다음과 같다.
- 일반 메서드
- 속성의 get/set
- 인덱서의 get/set
- 생성자
- 종료자
- 이벤트의 add/remove
✍ 샘플코드
class Vector
{
double x;
double y;
public Vector(double x) => this.x = x; // 생성자
public Vector(double x, double y) //생성자이지만, 2개의 문장이므로 람다식 불가
{
this.x = x;
this.y = y;
}
~Vector() => Console.WriteLine("Bye~ Vector");// 종료자
public double X => x; //속성의 get(읽기전용)
public double Y
{
get => y;
set => this.y = value; //속성의 set
}
public double this[int index]
{
get => (index == 0) ? this.x : this.y; //인덱서의 get
set => _ = (index == 0) ? this.x = value : this.y = value; //인덱서의 set
}
private EventHandler positionChanged;
public event EventHandler PositionChanged
{
add => this.positionChanged += value; //이벤트의 get
remove => this.positionChanged -= value; //이벤트의 set
}
public void PrintIt() => Console.WriteLine(this.x + ", " +this.y); //일반 메서드
}