System.Collections.Specialized.NotifyCollectionChangedEventArgs

Overview

The NotifyCollectionChangedEventArgs class provides data for the NotifyCollectionChangedEventHandler event that occurs when a dynamic collection changes.

It is used by collection types that implement INotifyCollectionChanged, such as ObservableCollection<T>.

Syntax

public sealed class NotifyCollectionChangedEventArgs : EventArgs

Constructors

SignatureDescription
NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction action) Initializes a new instance with the specified action.
NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction action, object changedItem) Initializes a new instance with the action and the item that changed.
NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction action, IList newItems) Initializes a new instance with the action and the new items that were added.
NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction action, IList newItems, IList oldItems) Initializes a new instance with the action, the new items, and the items that were removed.
NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction action, object newItem, object oldItem) Initializes a new instance with the action, the new item, and the old item.
NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction action, IList newItems, int newStartingIndex, IList oldItems, int oldStartingIndex) Initializes a new instance with the action, the new items, new index, old items, and old index.
NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction action, object newItem, int newStartingIndex) Initializes a new instance with the action, a new item, and its starting index.
NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction action, IList newItems, int newStartingIndex) Initializes a new instance with the action, new items, and their starting index.
NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction action, object newItem, object oldItem, int index) Initializes a new instance for a replace action with a new item, old item, and index.
NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction action, IList newItems, IList oldItems, int startingIndex) Initializes a new instance for a replace action with multiple items.
NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction action, IList changedItems, int index) Initializes a new instance for a reset action with a list of items and index.

Properties

PropertyTypeDescription
Action NotifyCollectionChangedAction Gets the action that caused the event.
NewItems IList Gets the list of new items involved in the change.
OldItems IList Gets the list of items affected by a replace or remove action.
NewStartingIndex int Gets the index where the new items were inserted.
OldStartingIndex int Gets the index where the old items were located before the change.
NewItem object Gets the single new item involved in the change (if applicable).
OldItem object Gets the single old item involved in a replace or remove operation.
OldStartingIndex int Gets the index of the old items before the change.

Remarks

The NotifyCollectionChangedEventArgs object is passed to event handlers of collections that implement INotifyCollectionChanged. It conveys what kind of modification occurred (add, remove, replace, move, or reset) and provides the items and indices involved.

When raising the event, always use the most specific constructor that matches the change you are reporting. This helps subscribers correctly interpret the operation.

Example

This example demonstrates how an ObservableCollection<string> raises the CollectionChanged event and how you can use NotifyCollectionChangedEventArgs to respond.

using System;
using System.Collections.ObjectModel;
using System.Collections.Specialized;

class Program
{
    static void Main()
    {
        var fruits = new ObservableCollection<string>();
        fruits.CollectionChanged += OnCollectionChanged;

        fruits.Add("Apple");
        fruits.Add("Banana");
        fruits[0] = "Apricot";
        fruits.RemoveAt(1);
        fruits.Clear();
    }

    private static void OnCollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
    {
        Console.WriteLine($"\nAction: {e.Action}");

        if (e.NewItems != null)
        {
            foreach (var item in e.NewItems)
                Console.WriteLine($"\tNew: {item}");
        }

        if (e.OldItems != null)
        {
            foreach (var item in e.OldItems)
                Console.WriteLine($"\tOld: {item}");
        }
    }
}

See Also