c\#의뢰 와 이벤트 실례 학습

Common.cs:
 
using System;
using System.Collections.Generic;
using System.Text;
namespace DelegateAndEvent.App_Code
{
public class Common
{
// .
public static string txt = "";
#region
public string HelloCSharp(string name)
{
txt += "hello " + name;// .
return "hello " + name;
}
public string HiCSharp(string name)
{
txt += "hi " + name;
return "hi " + name;
}
#endregion
#region
// , delegate. , .
public delegate string SayHi(string name);
// . .
public SayHi dlgt1, dlgt2;
//
public void useDelegate(string name, SayHi sayHi)
{
sayHi(name);
}
#endregion
#region
//
public event SayHi hiEvent;
//
public void causeEvent()
{
hiEvent += HelloCSharp;
hiEvent += HiCSharp;
if (hiEvent != null)
{
hiEvent("crane");
}
}
#endregion
}
}
using System;
using System.Collections.Generic;
using System.Text;
namespace DelegateAndEvent.App_Code
{
public class Common
{
// .
public static string txt = "";
#region
public string HelloCSharp(string name)
{
txt += "hello " + name;// .
return "hello " + name;
}
public string HiCSharp(string name)
{
txt += "hi " + name;
return "hi " + name;
}
#endregion
#region
// , delegate. , .
public delegate string SayHi(string name);
// . .
public SayHi dlgt1, dlgt2;
//
public void useDelegate(string name, SayHi sayHi)
{
sayHi(name);
}
#endregion
#region
//
public event SayHi hiEvent;
//
public void causeEvent()
{
hiEvent += HelloCSharp;
hiEvent += HiCSharp;
if (hiEvent != null)
{
hiEvent("crane");
}
}
#endregion
}
}
MainFrm.cs:
 
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DelegateAndEvent.App_Code;
namespace DelegateAndEvent
{
public partial class MainFrm : Form
{
Common common = new Common();
public MainFrm()
{
InitializeComponent();
}
private void btnOk_Click(object sender, EventArgs e)
{
//this.lblShow.Text += common.HelloCSharp("tree");
//
common.dlgt1 = common.HelloCSharp;// , ()
common.dlgt1 += common.HiCSharp;// , .
//this.lblShow.Text += common.dlgt1("tree");// .
//this.lblShow.Text = Common.txt;
//
//common.useDelegate("tree", common.dlgt1);
//this.lblShow.Text = Common.txt;
//
/* common.hiEvent(); .
.
*/
common.causeEvent();
this.lblShow.Text = Common.txt;
}
}
}

좋은 웹페이지 즐겨찾기