Southsouth
Southsouth

Reputation: 2695

Does the List<T> class declare a single event member called Changed?

An example of event in C# 4.0 Spec here says that

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

I can't find the event Changed by Reflector.

Upvotes: 0

Views: 98

Answers (4)

Mark Sowul
Mark Sowul

Reputation: 10600

If you are really looking for such a list, try BindingList<T> (which has ListChanged) or ObservableCollection<T>

Upvotes: 0

Alexei Levenkov
Alexei Levenkov

Reputation: 100547

The statement is true for List<T> class defined in the book. It has nothing to do with .Net Framework class System.Collection.Generic.List<T>.

And yes, if you would copy the class from the book it will have Changed event.

Upvotes: 2

Josh
Josh

Reputation: 44916

There is nothing in the documentation to indicate any events exist on a List<T>.

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

Upvotes: 1

Nikhil Agrawal
Nikhil Agrawal

Reputation: 48568

You could inherit from List and add your own handler, something like

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...");
        }
    }
}

Upvotes: 0

Related Questions