using System;using System.Collections.Generic;publicclassPersonEventArgs:EventArgs{
publicstring Name {
get;set;}}publicclassPublisher{
publicdelegatevoidContactNotifyEnvetHandler(object source,PersonEventArgs args);publiceventContactNotifyEnvetHandler ContactNotify;protectedvirtualvoidOnContactNotify(string person){
if(ContactNotify !=null){
ContactNotify(this,newPersonEventArgs(){
Name = person});}}publicvoidCountMessages(List<string> peopleList){
Dictionary<string,int> ContactList =newDictionary<string,int>();foreach(string person in peopleList){
if(ContactList.ContainsKey(person)){
ContactList[person]= ContactList[person]+1;if(ContactList[person]==3){
OnContactNotify(person);
ContactList[person]=0;}}else{
ContactList[person]=1;}}}}
答案2:
using System;using System.Collections.Generic;publicclassPersonEventArgs:EventArgs{
publicstring Name {
get;set;}}publicclassPublisher{
privatereadonly Dictionary<string,int> _names =newDictionary<string,int>();publicevent EventHandler<PersonEventArgs> ContactNotify;publicvoidCountMessages(List<string> peopleList){
foreach(var person in peopleList){
if(_names.ContainsKey(person)){
_names[person]++;if(_names[person]%3==0){
OnContactNotify(newPersonEventArgs{
Name = person });}}else{
_names[person]=1;}}}protectedvirtualvoidOnContactNotify(PersonEventArgs e){
ContactNotify