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 u
sedelegate(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
}
}