0

C# 4.0 Spec here中的一个事件示例说

"The List<T> class declares a single event member called Changed, which indicates that a new item has been added to the list. The Changed event is raised by the OnChanged virtual method, which first checks whether the event is null (meaning that no handlers are present). The notion of raising an event is precisely equivalent to invoking the delegate represented by the event—thus, there are no special language constructs for raising events."

我无法Changed通过 Reflector 找到该事件。

4

4 回答 4

2

该陈述对于List<T>书中定义的类是正确的。它与 .Net Framework 无关class System.Collection.Generic.List<T>

是的,如果您从书中复制课程,它将具有 Changed 事件。

于 2012-05-12T02:17:31.783 回答
1

文档中没有任何内容表明List<T>.

http://msdn.microsoft.com/en-us/library/6sh2ey19.aspx

于 2012-05-12T02:13:01.493 回答
0

您可以从 List 继承并添加自己的处理程序,例如

using System;
using System.Collections.Generic;

namespace test {
    class Program {

        class MyList<T> : List<T> 
        {
            public event EventHandler OnAdd;

            public void Add(T item) 
            {
                if (null != OnAdd)
                    OnAdd(this, null);

                base.Add(item);
            }
        }

        static void Main(string[] args) 
        {
            MyList<int> l = new MyList<int>();
            l.OnAdd += new EventHandler(l_OnAdd);
            l.Add(1);
        }

        static void l_OnAdd(object sender, EventArgs e) 
        {
            Console.WriteLine("Element added...");
        }
    }
}
于 2012-05-12T02:12:19.353 回答
0

如果您真的在寻找这样的列表,请尝试BindingList<T>(具有 ListChanged)或ObservableCollection<T>

于 2012-05-12T03:01:41.033 回答