using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace FakeInventory
{
public class App
{
//변수 정의할 때 값
//메서드 정의 기능 생각
//메서드에 대한 참조를 가질 수 있는 형식
//한정자 delegate 형식명 연결할메서드시그니쳐
//2. 대리자 형식 정의
delegate void MyDelegate();
//생성자
public App()
{
//3. 대리자 인스턴스 생성 및 연결 (대리자 초기화)
MyDelegate myDel = new MyDelegate(this.SayHello);
//4. 대리자 호출 (연결된 메서드 호출)
myDel()
}
//1. 메서드 정의 (시그니처가 대리자형식과 같은)
//안녕하세요를 출력하는 메서드
void SayHello()
{
Console.WriteLine("안녕하세요");
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace FakeInventory
{
public class App
{
//2. 대리자 형식 정의
delegate void MyDelegate(string name);
//생성자
public App()
{
//3. 대리자 인스턴스화
MyDelegate myDel = new MyDelegate(this.SayHello);
//4. 대리자 호출
myDel("홍길동");
}
//1. 메서드 정의
void SayHello(string name)
{
Console.WriteLine("{0}님 안녕하세요~", name);
}
void ByebBye(string name)
{
Console.WriteLine("{0}님 안녕히 가세요", name);
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace FakeInventory
{
public class App
{
//2. 대리자 형식 정의
delegate int myDelegate(int a, int b);
//생성자
public App()
{
//3. 대리자 인스턴스화
myDelegate myDel = new myDelegate(this.Sum);
//4. 대리자 호출
Console.WriteLine(myDel(1, 2));
}
//1. 메서드 정의
int Sum(int a, int b)
{
return a + b;
}
}
}
App
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace FakeInventory
{
public class App
{
//2. 대리자 형식 정의
public delegate void MyDel();
//생성자
public App()
{
//3. 대리자 인스턴스화(메서드연결)
MyDel myDel = new MyDel(this.HeroMoveComplete);
Hero hero = new Hero();
hero.Move(myDel); //HeroMoveComplete가 연결된 대리자 인스턴스를 인수로 전달
}
//1. 메서드 정의
void HeroMoveComplete()
{
Console.WriteLine("[App] 영웅이 이동을 완료했습니다.");
}
}
}
Hero
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace FakeInventory
{
internal class Hero
{
//생성자
public Hero()
{
Console.WriteLine("영웅이 생성되었습니다.");
}
public void Move(App.MyDel callback)
{
Console.WriteLine("이동했습니다.");
//4. 대리자 호출
callback();
}
}
}
무명메서드
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace FakeInventory
{
public class App
{
//1. 대리자 형식 정의
delegate void MyDel();
//생성자
public App()
{
//2. 대리자 인스턴스화(무명메서드 연결)
MyDel del = delegate () { //대리자 인스턴스 ,, 무명메서드와 연결된
Console.WriteLine("안녕하세요");
};
//3. 대리자 호출
del();
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace FakeInventory
{
public class App
{
//1. 대리자 형식 정의
delegate void MyDel(string name);
//생성자
public App()
{
//2. 대리자 인스턴스화
MyDel del = delegate (string name) {
Console.WriteLine("{0}님 안녕하세요", name);
};
//3. 대리자 호출
del("홍길동");
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace FakeInventory
{
public class App
{
//1. 대리자 형식 정의
delegate int MyDel(int a, int b);
//생성자
public App()
{
//2. 대리자 인스턴스화
MyDel del = delegate (int a, int b) {
return a + b;
};
//3. 대리자 호출
Console.WriteLine(del(1, 2));
}
}
}
'C#프로그래밍' 카테고리의 다른 글
엑셀데이터를 JSON, JSON online viewer (0) | 2023.07.27 |
---|---|
람다, Action<T> , Func<T> (0) | 2023.07.27 |
List, Dictionary 복습 (0) | 2023.07.27 |
과제 (0) | 2023.07.26 |
디자인패턴/싱글톤패턴/DataManager, 개체 이니셜라이저 (0) | 2023.07.26 |