반응형
- 클래스와 메서드 이름 앞에 static 키워드가 붙음
- 확장 메서드의 첫 번째 파라미터는 this 키워드 + 데이터 형식(int, string, ...) 또는 클래스가 붙음
public static class ExtensionMethodTest
{
public static int PlusPlus(this int a)
{
return a + a;
}
public static int ForPlus(this List<short> lists)
{
int sum = 0;
lists.ForEach(x => sum += x);
return sum;
}
}
int i = 10;
Console.WriteLine($"int의 확장메서드: {i.PlusPlus()}");
List<short> s = new List<short>() { 1, 2, 3 };
Console.WriteLine($"List<short>의 확장메서드: {s.ForPlus()}");
반응형
'C#' 카테고리의 다른 글
C# 리플렉션(reflection) (0) | 2021.12.30 |
---|---|
C# asyn, await (0) | 2021.12.27 |
C# 이벤트(Event) (0) | 2021.12.26 |
C# 대리자(Delegate), 무명 메서드, 람다식 (0) | 2021.12.26 |
C# 제네릭(Generic) (0) | 2021.12.24 |