티스토리 뷰
Delegate(델리게이트)로 선언되어 있는 Action, Func 대리자에 대한 간단한 고찰 및 예제~~ 자세한 내용은 MSDN을 참조 -> http://msdn.microsoft.com/ko-kr/library/System(v=vs.110).aspx 무명메소드를 이용하거나, 람다식으로도 사용이 가능하지만, 현재 포스트는 그냥 기본사용 방법에 대해서만 소개한다. Action - 파라미터의 수에따라 0개부터 최대 16개의 파라미터까지 받을 수 있다. - 주의할점은 리턴값이 없어야 한다.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace Delegate_Action_Func
{
public partial class SampleAction : Form
{
public SampleAction()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
/* Delegate Action
입력 : T 타입
Action
Action
Action
Action
Action
Action<최대 16개 Type 까지가능>
리턴값 : 없음
*/
Action act = null;
if (textBox1.Text == "준비")
{
act = Ready;
}
else if (textBox1.Text == "시작")
{
act = Start;
}
else if (textBox1.Text == "중지")
{
act = Stop;
}
if (act.Target != null)
act(textBox1.Text);
}
private void Ready(string s){
MessageBox.Show(s);
}
private void Start(string s){
MessageBox.Show(s);
}
private void Stop(string s){
MessageBox.Show(s);
}
}
}
Func - 파라미터 수에따라 0개부터 최대 16개의 파라미터까지 받을 수 있다. - Action 과는 달리 리턴값이 있어야 한다.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace Delegate_Action_Func
{
public partial class SampleFunc : Form
{
public SampleFunc()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
/* Delegate Func
입력 : T 타입
Func
Func
Func
Func
Func
Func<최대 16개 Type 까지가능>
리턴값 : 무조건 있어야함
*/
Func fnc = null;
if (textBox1.Text == "준비")
{
fnc = Ready;
}
else if (textBox1.Text == "시작")
{
fnc = Start;
}
else if (textBox1.Text == "중지")
{
fnc = Stop;
}
if(fnc.Target != null)
{
//Func는 함수호출방식으로 호출하면 리턴값을 전달한다
string msg = fnc();
MessageBox.Show(msg);
}
}
private string Ready(){
return "준비";
}
private string Start(){
return "시작";
}
private string Stop(){
return "정지";
}
}
} 델리게이트, c#, Action, Func
'프로그래밍 > .NetFramework' 카테고리의 다른 글
| 대량의 데이터를 한번에 인서트하자 Bulk Insert (0) | 2015.01.21 |
|---|---|
| SHA-256 + Base64 Encrypt (암호화) (0) | 2014.04.10 |
| BackgroundWorker 사용법 (0) | 2014.02.10 |
| 폴더복사 (0) | 2014.02.10 |
| C#에서 Internet explorer(IE) COM 객체로 핸들링하기. Win32API이용 (0) | 2013.05.06 |
댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
- Total
- Today
- Yesterday
TAG
- 프로시저
- CSS
- jQuery
- json
- JavaScript
- Excel
- 제이쿼리
- html5
- grid
- Style
- Chart
- WebApi
- JS
- Ajax
- css3
- MSSQL
- jQuery Mobile
- IE
- rowspan
- workbook
- Mobile
- 자바스크립트
- radius
- jquery chart
- 셀렉터
- drag&drop
- SVG
- WCF
- 저장프로시저
- ASP.NET
| 일 | 월 | 화 | 수 | 목 | 금 | 토 |
|---|---|---|---|---|---|---|
| 1 | ||||||
| 2 | 3 | 4 | 5 | 6 | 7 | 8 |
| 9 | 10 | 11 | 12 | 13 | 14 | 15 |
| 16 | 17 | 18 | 19 | 20 | 21 | 22 |
| 23 | 24 | 25 | 26 | 27 | 28 | 29 |
| 30 |
글 보관함