ron ron - 11 months ago 53
C# Question

C# - CollectionChange doesn't detect change in values

I am new in C# and want to detect if my List was changed.
Based on my research I should use ObservableCollection instead of List.
Basically, I want to check if I already assigned new values to my ObservableCollection.

Here is a sample code similar to mine:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;
using System.Collections.ObjectModel;
namespace Rextester
public class Program
public static void Main(string[] args)
ObservableCollection<string> strList = new ObservableCollection<string>{"1", "2"};
strList.CollectionChanged += onCollectionChange;
ObservableCollection<string> strList2 = new ObservableCollection<string>{"1", "2", "3"};
strList = strList2;

public static void onCollectionChange(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e){

It should print "Changed" when I assign strList2 to strList right?
I noticed that when I un comment the
, "Changed" will not be printed. What am I doing wrong here? Please help. I tested the codes at

Thanks in advance.

Answer Source

strList Initially points to a ObservableCollection to which you hook up the CollectionChanged event.

After that, you let point strList to a new ObservableCollection. So you don't change the collection, you just don't use it anymore.