List <t>类是否声明了名为Changed?</t>的单个事件成员

时间:2012-05-12 02:08:37

标签: c#

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."

我无法通过Reflector找到事件Changed

4 个答案:

答案 0 :(得分:2)

该陈述适用于本书中定义的List<T>类。它与.Net Framework class System.Collection.Generic.List<T>无关。

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

答案 1 :(得分:1)

文档中没有任何内容表明List<T>上存在任何事件。

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

答案 2 :(得分: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...");
        }
    }
}

答案 3 :(得分:0)

如果您确实在寻找此类列表,请尝试BindingList<T>(具有ListChanged)或ObservableCollection<T>