C # An observed set of Sets,

Source: Internet
Author: User

C # An observed set of Sets,

If you want to delete or add elements in the set, you can use the ObservableCollection <T> class. This class is defined for WPF, so that the UI can know the changes to the set. This class is defined in Assembly WindowsBase and needs to be referenced.
The ObservableCollection <T> class derives from the Collection <T> base class. This base class can be used to create a custom set and internally use the List <T> class. Override the Virtual Methods SetItem () and RemoveItem () of the base class to trigger the CollectionChanged event.

    static void Main()        {          var data = new ObservableCollection<string>();          data.CollectionChanged += Data_CollectionChanged;          data.Add("One");          data.Add("Two");          data.Insert(1, "Three");          data.Remove("One");        }        static void Data_CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)        {          Console.WriteLine("action: {0}", e.Action.ToString());          if (e.OldItems != null)          {            Console.WriteLine("starting index for old item(s): {0}", e.OldStartingIndex);            Console.WriteLine("old item(s):");            foreach (var item in e.OldItems)            {              Console.WriteLine(item);            }          }          if (e.NewItems != null)          {            Console.WriteLine("starting index for new item(s): {0}", e.NewStartingIndex);            Console.WriteLine("new item(s): ");            foreach (var item in e.NewItems)            {              Console.WriteLine(item);            }          }          Console.WriteLine();        }

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.