Search code examples
c#.netobservablecollectioninotifycollectionchanged

How can an ObservableCollection fire a Replace action?


In the documentation of the event args of NotifyCollectionChangedEventArgs, there is an action called Replace (in addition to Add, Remove, Move, etc.). When can this be fired? I can't see any Replace method in ObservableCollection


Solution

  • Here is an example:

    ObservableCollection<string> myCollection = new ObservableCollection<string>;
    myCollection.Add("One");
    myCollection.Add("Two");
    myCollection.Add("Three");
    myCollection.Add("Four");
    myCollection.Add("Five");
    
    myCollection[4] = "Six"; // Replace (i.e. Set)