Programming/C#
[C#] 확장메소드
홍상길
2017. 7. 20. 09:19
기존의 Class에 추가로 함수를 추가 하는경우 사용한다.
확장 메소드의 Class는 Static으로 정의 되어야 하며 첫번째 파마메터에 this 기존클래스명 이 들어가야 한다.
public static class StringExtention
{
public static int ToInt(this string str)
{
int result = 0;
try
{
result = int.Parse(str);
}
catch
{
}
return result;
}
}
확장 메소드 등록 시 아래처럼 상용이 가능하다.