반응형
SMALL
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Study10
{
class App
{
//2. 대리자 형식 정의
delegate void MyDel(string contents);
//생성자
public App()
{
//3. 대리자 변수 정의
//MyDel myDel;
//4. 대리자 인스턴스 생성 (메서드 연결), 변수에 할당
//myDel = new MyDel(this.Print);
this.LoadFile(null); //대리자 변수에 null값 허용
}
private void LoadFile(MyDel myDel)
{
//파일 읽기
Console.WriteLine("파일 읽는중...");
//파일을 다 읽음 (이벤트) : LOAD_COMPLETE (상태)
//대리자를 호출
string contents = "hello world!"; //<---- 파일에서 나온 문자열
//callback 메서드 호출, 대리자 호출, 대리자 메서드 호출
//null check 필요
if (myDel == null)
{
//출력 안하겠다
}
else
{
myDel(contents); //callback (Print), 출력 하겠다
}
}
//1. 대리자에 연결할 메서드 정의
private void Print(string contents)
{
//출력
Console.WriteLine(contents);
}
}
}
대리자 없이 사용
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Study10
{
class App
{
private string contents;
public App()
{
string contents = this.LoadFile();
this.Print(contents);
}
private string LoadFile() //파일을 읽는 기능
{
return "Hello World!";
}
private void Print(string contents) //파일을 출력하는 기능
{
Console.WriteLine(contents);
}
}
}
대리자 사용 1
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Study10
{
class App
{
//2. 대리자 형식 정의
delegate void MyDel(string contents);
//생성자
public App()
{
this.LoadFile();
}
private void LoadFile()
{
//파일 읽기
Console.WriteLine("파일 읽는중...");
//파일을 다 읽음 (이벤트) : LOAD_COMPLETE (상태)
//대리자를 호출
string contents = "hello world!"; //<---- 파일에서 나온 문자열
//3. 대리자 변수 정의
MyDel myDel;
//4. 대리자 인스턴스 생성 (메서드 연결), 변수에 할당
myDel = new MyDel(this.Print);
//callback 메서드 호출, 대리자 호출, 대리자 메서드 호출
myDel(contents);
}
//1. 대리자에 연결할 메서드 정의
private void Print(string contents)
{
//출력
Console.WriteLine(contents);
}
}
}
대리자 사용 2
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Study10
{
class App
{
//2. 대리자 형식 정의
delegate void MyDel(string contents);
//생성자
public App()
{
this.LoadFile();
}
private void LoadFile()
{
//파일 읽기
Console.WriteLine("파일 읽는중...");
//파일을 다 읽음 (이벤트) : LOAD_COMPLETE (상태)
//대리자를 호출
string contents = "hello world!"; //<---- 파일에서 나온 문자열
//3. 대리자 변수 정의
MyDel myDel;
//4. 대리자 인스턴스 생성 (메서드 연결), 변수에 할당
myDel = new MyDel(this.Print);
//callback 메서드 호출, 대리자 호출, 대리자 메서드 호출
myDel(contents);
}
//1. 대리자에 연결할 메서드 정의
private void Print(string contents)
{
//출력
Console.WriteLine(contents);
}
}
}
대리자 사용 3 (메서드 매개변수에 대리자 넣기 )
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Study10
{
class App
{
//2. 대리자 형식 정의
delegate void MyDel(string contents);
//생성자
public App()
{
//3. 대리자 변수 정의
MyDel myDel;
//4. 대리자 인스턴스 생성 (메서드 연결), 변수에 할당
myDel = new MyDel(this.Print);
this.LoadFile(myDel);
}
private void LoadFile(MyDel myDel)
{
//파일 읽기
Console.WriteLine("파일 읽는중...");
//파일을 다 읽음 (이벤트) : LOAD_COMPLETE (상태)
//대리자를 호출
string contents = "hello world!"; //<---- 파일에서 나온 문자열
//callback 메서드 호출, 대리자 호출, 대리자 메서드 호출
myDel(contents);
}
//1. 대리자에 연결할 메서드 정의
private void Print(string contents)
{
//출력
Console.WriteLine(contents);
}
}
}
대리자 사용 4
반응형
LIST
'C# > 수업 내용' 카테고리의 다른 글
람다문 사용 예시 (0) | 2023.01.11 |
---|---|
오후 수업내용 : 익명 메서드 (0) | 2023.01.11 |
Cal 연습문제 (대리자) (0) | 2023.01.11 |
오전 수업내용 : 대리자 (0) | 2023.01.11 |
2023/01.10 문자열 형식 오전 수업내용 (0) | 2023.01.11 |