Permalink
Cannot retrieve contributors at this time
Name already in use
A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
School/C# - Beginner (Denis)/Lesson 75/lesson_75.cs
Go to fileThis commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
57 lines (50 sloc)
1.89 KB
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.ObjectModel; | |
using System.Collections.Specialized; | |
namespace HelloApp | |
{ | |
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
ObservableCollection<User> users = new ObservableCollection<User> | |
{ | |
new User { Name = "Bill"}, | |
new User { Name = "Tom"}, | |
new User { Name = "Alice"} | |
}; | |
users.CollectionChanged += Users_CollectionChanged; | |
users.Add(new User { Name = "Bob" }); | |
users.RemoveAt(1); | |
users[0] = new User { Name = "Anders" }; | |
foreach (User user in users) | |
{ | |
Console.WriteLine(user.Name); | |
} | |
Console.Read(); | |
} | |
private static void Users_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e) | |
{ | |
switch (e.Action) | |
{ | |
case NotifyCollectionChangedAction.Add: // если добавление | |
User newUser = e.NewItems[0] as User; | |
Console.WriteLine($"Добавлен новый объект: {newUser.Name}"); | |
break; | |
case NotifyCollectionChangedAction.Remove: // если удаление | |
User oldUser = e.OldItems[0] as User; | |
Console.WriteLine($"Удален объект: {oldUser.Name}"); | |
break; | |
case NotifyCollectionChangedAction.Replace: // если замена | |
User replacedUser = e.OldItems[0] as User; | |
User replacingUser = e.NewItems[0] as User; | |
Console.WriteLine($"Объект {replacedUser.Name} заменен объектом {replacingUser.Name}"); | |
break; | |
} | |
} | |
} | |
class User | |
{ | |
public string Name { get; set; } | |
} | |
} |