개발 이야기/Visual C#2008. 3. 17. 14:47

public class SamplesDelegate
{
    public delegate String myMethodDelegate(int myInt); // 클래스와 동급으로 정의

    public class mySampleClass
    {
        // 인스턴스 메서드 선언
        public String myStringMethod(int myInt)
        {
            if(myInt > 0)
                return "positive";
            if (myInt < 0)
                return "negative";

            return "zero";
        }

        // 정적 메서드 선언
        public static String mySignMethod(int myInt)
        {
            if (myInt > 0)
                return ("+");

            if (myInt < 0)
                return ("-");

            return ("");
        }
    }

    public static void Main()
    {
        // 클래스 선언
        mySampleClass mClass = new mySampleClass();

        // 각 델리케이트 선언
        myMethodDelegate mDele1 = new myMethodDelegate(mClass.myStringMethod);  // 인스턴스 메서드시
        myMethodDelegate mDele2 = new myMethodDelegate(mySampleClass.mySignMethod); // 정적 메서드시

        Console.WriteLine("{0} is {1}; use the sign \"{2}\".", 5, mDele1(5), mDele2(5));
        Console.WriteLine("{0} is {1}; use the sign \"{2}\".", -3, mDele1(-3), mDele2(-3));
        Console.WriteLine("{0} is {1}; use the sign \"{2}\".", 0, mDele1(0), mDele2(0));

        Console.ReadKey();
    }
}

Posted by 사나에