Post

C# 확장메서드

Extension Method

Git Source


확장 메서드란? 🔨

  • 어떠한 클래스에 메서드를 추가하고 싶을 때 클래스를 수정하거나 파생 클래스를 만들지 않고 추가하는 방법이다.
  • 확장메서드는 static 클래스 및 메서드로 정의되어야하고, 확장 메서드의 첫 번째 파라미터는 확장 메서드가 확장하려고하는 클래스의 식별자로 정의하며 this 예약어를 붙여야한다.
  • 두 번째 파라미터가 없을 경우는 생략이 가능하다.
  • static 메서드이지만 사용은 인스턴스 메서드처럼 사용된다.
  • 확장 메서드의 대표적으로 LINQ가 있다.

확장 메서드 사용

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
static class ExtensionClass
{
    public static int Factorial(this int i)
    {
        if (i < 2)
            return 1;
        else
            return i * Factorial(i - 1);
    }

    public static string StringConcat(this string str, string str2)
    {
        return str + str2;
    }
}
class Program
{
    static void Main(string[] args)
    {
        int inumber;
        Console.Write("Factorial : ");
        if (int.TryParse(Console.ReadLine(), out inumber))
            Console.WriteLine(inumber.Factorial().ToString());
        else
            Console.WriteLine("Input Error"); 

        string str = "Hello"; 
        Console.WriteLine(str.StringConcat(" World"));
    }
}
1
2
3
4
5
결과:
Factorial : 6
720
Hello World
계속하려면 아무 키나 누르십시오 . . .

IntelliSense 🔖

  • 확장 메서드를 만들게 되면 다음과 같이 박스형태와 화살표로 이루어져 있다.

extension

  • LINQ (Select, Where 등)

linq

This post is licensed under CC BY 4.0 by the author.