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
'프로그래밍(Programming) > C#' 카테고리의 다른 글
열거형 GetEnumerator(), IEnumerable, IEnumerator 정의 (0) | 2015.09.01 |
---|---|
delegate 와 event 차이 , 이벤트 인스턴스 저장 (0) | 2015.09.01 |
delegate 위임이란, 간략 사용법 (0) | 2015.08.31 |
[c#]class 앞에 붙는 public 이란? (0) | 2015.08.28 |
[C#]정적 생성자(static constructor) (0) | 2015.08.28 |