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;
//strList.Clear();
ObservableCollection<string> strList2 = new ObservableCollection<string>{"1", "2", "3"};
strList = strList2;
}
public static void onCollectionChange(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e){
Console.WriteLine("Changed");
}
}
}
It should print "Changed" when I assign strList2 to strList right?
I noticed that when I un comment the strList.Clear();
, "Changed" will not be printed. What am I doing wrong here? Please help. I tested the codes at http://rextester.com/
Thanks in advance.