C#문법연습(14): 클래스[6] - 이벤트

1004 단어 C#


using System;



class MyClass

{

    private int FNum;



    public delegate void MyDelegate(int n); /*          */

    public event MyDelegate MyEvent;        /*   event                */



    /*       Num         */

    public int Num 

    {

        get { return FNum; }

        set 

        { 

            FNum = value;

            if (MyEvent != null) MyEvent(FNum);

        }     

    }

}



class Program

{

    static void Main()

    {

        MyClass obj = new MyClass();



        /*               : Event_Method */

        obj.MyEvent += new MyClass.MyDelegate(Event_Method);



        obj.Num = 5; /*         : 5     */

        obj.Num = 6; /*         : 6     */



        Console.ReadKey();

    }



    /*              */

    static void Event_Method(int n)

    {

        if (n % 2 == 0) 

            Console.WriteLine("{0}    ", n); 

        else 

            Console.WriteLine("{0}    ", n);

    }

}


 
   

좋은 웹페이지 즐겨찾기