티스토리 뷰

 

 
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


댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
«   2024/05   »
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 31
글 보관함