Archived
1
0
This repository has been archived on 2026-03-24. You can view files and clone it. You cannot open issues or pull requests or push a commit.
Files
SomeLab/cs5/5_7/Program.cs
2025-11-15 20:25:57 +08:00

54 lines
1.5 KiB
C#

using System;
using System.Collections;
namespace _5_7
{
public class NameListEventArgs : EventArgs
{
public string Name { get; set; }
public int Count { get; set; }
public NameListEventArgs(string name, int count)
{
Name = name;
Count = count;
}
}
public delegate void NameListEventHandler(object source, NameListEventArgs e);
public class NameList
{
ArrayList list;
public event NameListEventHandler nameListEvent;
public NameList()
{
list = new ArrayList();
}
public void Add(string name)
{
list.Add(name);
if (nameListEvent != null)
{
nameListEvent(this, new NameListEventArgs(name, list.Count));
}
}
}
public class EventDemo
{
public static void Method1(object source, NameListEventArgs e)
{
Console.WriteLine("增加项目 {0}", e.Name);
}
public static void Method2(object source, NameListEventArgs e)
{
Console.WriteLine("当前项目总数 {0}", e.Count);
}
public static void Main()
{
NameList myList = new NameList();
myList.nameListEvent += new NameListEventHandler(Method1);
myList.nameListEvent += new NameListEventHandler(Method2);
myList.Add("张三");
myList.Add("李四");
myList.Add("王五");
}
}
}