본문 바로가기

Etc/C#

Extension Method


사실 Extension Method에 대해서는 작년 초쯤에 공팀장님으로 부터 설명을 들었었는데

그때 듣고 아항~ 하고 넘어 갔더니 지금은 기억이... 금붕금붕 -.,-;;;

그래서 다시 찾아보고 아~ 포스팅(메모) 를 해야 겠구나 싶어서 하는 포스팅(메모)~ -.,-;;

class Program

    {

        static void Main(string[] args)

        {

            string s = "Hello World";

            int i = s.WordCount();

 

            Console.WriteLine(i.ToString());

        }

    }

 

    //Static Class 여야 하고

    public static class baby

    {

        //Static Method여야 하고

        //parameter this type 을 정해줘야 하는것 같음 -.,-;;

        public static int WordCount(this string str)

        {

            return str.Split(new char[] { ' ', '.', '?' }, StringSplitOptions.RemoveEmptyEntries).Length;

        }

    }




맞나용? -.,-;;