????1.5 ???????
??????????????е?????????????????????????????衣????????????????????????????γ???????????Action??Func??Predicate????????????????裬?????á?
????C#
public class Program
{
public static void Main(string[] args)
{
//Action
Actionstring> action = delegate(string str) { Console.WriteLine("???!" + str); };
action("GG");
//Func
Funcint?? int?? int> func = delegate(int x?? int y) { return x + y; };
Console.WriteLine("????????" + func(5?? 6));
//Predicate
Predicatebool> per = delegate(bool isTrue) { return isTrue == true; };
Console.WriteLine(per(true));
}
}
????????????????£?
????Action??У?????????????ж?????????????з??????
????Func??У?????????????ж?????????????з??????
????Predicate??У?????????????????????????????????bool?????
????2. ???
????????????????????????????У??????????????????е?????????????????????????????????????????п?????????λ?????????????????????????????????????????????е??á??????????????????????????С?
????2.1 ???????
?????????????????????????????????
????C#
///
/// ?????????
///
public class MusicPlayer
{
//step01?????? ?????????? ???
public event EventHandler PlayOverEvent;
public string Name { get; set; }
public MusicPlayer(string name)
{
this.Name = name;
}
//step02?????????????????????
public void PlaySong()
{
//?????
Console.WriteLine("????????????" + this.Name);
for (int i = 0; i 20; i++)
{
Console.Write(".");
Thread.Sleep(100);
}
//?????????????PlayOverEvent???
if (PlayOverEvent != null)
{
PlayOverEvent(this?? null);
}
}
}
public class Program
{
static void Main(string[] args)
{
//?????????????????
MusicPlayer player = new MusicPlayer("???????");
//step03????????
player.PlayOverEvent += player_PlayOverEvent;
//????????????????????
player.PlaySong();
Console.ReadKey();
}
static void player_PlayOverEvent(object sender??EventArgs e)
{
MusicPlayer player = sender as MusicPlayer;
Console.WriteLine("rn{0}???????"?? player.Name);
}
}
???????????н????
??????1??????????????????????裺
????step01????event????????????????????????????????????
????step02????????????崥????????????
????step03?????????????????????????
??????2??public event EventHandler PlayOverEvent
????????????MusicPlayer???????????????PlayOverEvent??????????????????С?????е??????????????EventHandler???????????????????????????????£?
????EventHandler???????????????У????????з???????????????????sender???????????????????????????TEventArgs??????????????????????????????System.EventArgs??
????2.2 ????????
????C#
????MusicPlayer player = new MusicPlayer("???????");
????//??????
????player.PlayOverEvent += player_PlayOverEvent;
????player.PlaySong();
????????????????????????????”+=”????????????????????????????????????Delegate.Combine???????????????????????????????????£?
???????????PlayOverEvent??????????????????????add_ PlayOverEvent??remove_ PlayOverEvent??add???????????Delegate.Combine????????????????????б??remove???????????Delegate.Remove??????б??????????????????????????????????????С?