using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Collections.Specialized;
public static void Main()
var people = new ObservableCollection<string>(new string[] { "Mike", "Alice", "Kate" })
Console.WriteLine(people[0]);
foreach (var person in people)
Console.WriteLine(person);
for (int i = 0; i < people.Count; i++)
Console.WriteLine((i + 1) + "" + people[i]);
var people1 = new ObservableCollection<Person>()
people1.CollectionChanged += People_CollectionChanged;
people1.Add(new Person("Bob"));
people1[0] = new Person("Eugene");
Console.WriteLine("\nСписок пользователей:");
foreach (var person in people1)
Console.WriteLine(person.Name);
void People_CollectionChanged(object? sender, NotifyCollectionChangedEventArgs e)
case NotifyCollectionChangedAction.Add:
if (e.NewItems?[0] is Person newPerson)
Console.WriteLine($"Добавлен новый объект: {newPerson.Name}");
case NotifyCollectionChangedAction.Remove:
if (e.OldItems?[0] is Person oldPerson)
Console.WriteLine($"Удален объект: {oldPerson.Name}");
case NotifyCollectionChangedAction.Replace:
if ((e.NewItems?[0] is Person replacingPerson) && (e.OldItems?[0] is Person replacedPerson))
Console.WriteLine($"Объект {replacedPerson.Name} заменен объектом {replacingPerson.Name}");
public string Name { get; }
public Person(string name) => Name = name;