using System;
using System.Collections.Generic;
using System.Collections;
using System.Linq;
using System.Text;
using System.Threading.Tasks;


namespace TestE
{
    class DBConnection {

        protected static int NextConnectionNbr = 1;

        protected string connectionName;
        public string ConnectionName{ get { return connectionName; } }

        public DBConnection()
        {
            connectionName = "Database Connection " + DBConnection.NextConnectionNbr++;
        }

    }


    class DBManager
    {
        protected ArrayList activeConnetions;
        public DBManager()
        {
            activeConnetions = new ArrayList ();
            for (int i = 0; i < 6; ++i) {
                activeConnetions.Add (new DBConnection ());

            }
        }

        public delegate void EnumConnectionsCallback(DBConnection connection);            

//delegate 선언, 이 함수 타입과 동일한 함수를  나중에 담을 수 있게 된다

        public void EnumConnections(EnumConnectionsCallback callback)                     
        {
            foreach(DBConnection connection in activeConnetions)
            {
                callback(connection);
            }
        }
    }



    class Program {

        public static void PrintConnections(DBConnection connection)
        {
            Console.WriteLine ("[InstanceDelegate.PrintConnections] {0}", connection.ConnectionName);
        }

        static int Main(string[] args)
        {

            DBManager dbMsanager = new DBManager ();

            DBManager.EnumConnectionsCallback printConnections = new DBManager.EnumConnectionsCallback

//delegate 와 동일한 함수 타입의 함수인  PrintConnections 를 담는다

            dbMsanager.EnumConnections (printConnections);

            Console.ReadLine ();

            return 0;
        }
    }
}




결과


[InstanceDelegate.PrintConnections] Database Connection 1

[InstanceDelegate.PrintConnections] Database Connection 2

[InstanceDelegate.PrintConnections] Database Connection 3

[InstanceDelegate.PrintConnections] Database Connection 4

[InstanceDelegate.PrintConnections] Database Connection 5

[InstanceDelegate.PrintConnections] Database Connection 6



- 참고 inside c# second

반응형

+ Recent posts