Programming/C# (vs Java)

확장 메서드

Lawmin 2012. 11. 20. 10:20

class 상속이나 변경 없이 특정 namespace 에서 별도로 구현한 메서드를 기존 클래스의 메서드로 사용가능한 기능


namespace Ext

{

    public static class IntExt

    {

        public static int Mod(this int myInt, int divisor)

        {

            return myInt % divisor;

        }


        public void test()

        {

            int a = 10;

            a.Mod(5);

        }

    }

}