C#依頼およびイベント
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace
{
class Program
{
static void Main(string[] args)
{
Emplyee em = new Emplyee();
Reporter re = new Reporter();
// ,
em.PlayGame+=new DelegateClassHandle(re.Notify);
em.Game();
Console.ReadKey();
}
}
public delegate void DelegateClassHandle(); //
public class Emplyee //
{
public event DelegateClassHandle PlayGame; // PlayGame
public void Game() // Game
{
if (PlayGame != null) //
{
Console.WriteLine(" , ");
PlayGame();
}
}
}
public class Reporter //
{
public void Notify()
{
Console.WriteLine(" , ");
}
}
パラメータ付き委任:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace
{
class Program
{
static void Main(string[] args)
{
Employee el = new Employee(" ",888);
Reporter re = new Reporter();
// ,
el.PlayGame+=new DelegateClassHandle(re.Notify);
el.Game();
Console.ReadKey();
}
}
/// <summary>
///
/// </summary>
/// <param name="sen"> </param>
/// <param name="e"> </param>
public delegate void DelegateClassHandle(object sen, cc e); //
public class Employee //
{
string _name;
int _number;
public string Name
{
get { return _name; }
set { _name = value; }
}
public int Number
{
get { return _number; }
set { _number = value; }
}
public event DelegateClassHandle PlayGame; // PlayGame
public Employee(string nm, int nu) {
_name = nm;
_number = nu;
}
public void Game() // Game
{
if (PlayGame != null) //
{
Console.WriteLine(" , ");
//
cc e = new cc();
e.Name = _name;
e.Number = _number;
PlayGame(this,e);
}
}
}
public class Reporter //
{
public void Notify(object sen, cc e)
{
Console.WriteLine(" ,{0} {1} ",e.Name,e.Number );
}
}
public class cc : EventArgs
{
public string Name = "";
public int Number = 0;
}
}