-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use top-level namespaces - Remove unused usings
- Loading branch information
1 parent
8bcebaa
commit cd5b5de
Showing
29 changed files
with
612 additions
and
671 deletions.
There are no files selected for viewing
49 changes: 24 additions & 25 deletions
49
ObservableCollection/MintPlayer.ObservableCollection.Test/Extensions/IEnumerable.cs
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,37 +1,36 @@ | ||
using System.Collections; | ||
|
||
namespace MintPlayer.ObservableCollection.Test.Extensions | ||
namespace MintPlayer.ObservableCollection.Test.Extensions; | ||
|
||
public static class IEnumerable | ||
{ | ||
public static class IEnumerable | ||
public static bool IsNullOrEmpty(this System.Collections.IList list) | ||
{ | ||
public static bool IsNullOrEmpty(this System.Collections.IList list) | ||
if (list == null) | ||
{ | ||
if (list == null) | ||
{ | ||
return true; | ||
} | ||
else | ||
{ | ||
return list.Count == 0; | ||
} | ||
return true; | ||
} | ||
public static bool IsNullOrEmpty<T>(this System.Collections.Generic.IList<T> list) | ||
else | ||
{ | ||
if (list == null) | ||
{ | ||
return true; | ||
} | ||
else | ||
{ | ||
return list.Count == 0; | ||
} | ||
return list.Count == 0; | ||
} | ||
|
||
public static T[] ToArray<T>(this IList list) | ||
} | ||
public static bool IsNullOrEmpty<T>(this System.Collections.Generic.IList<T> list) | ||
{ | ||
if (list == null) | ||
{ | ||
var array = new T[list.Count]; | ||
list.CopyTo(array, 0); | ||
return array; | ||
return true; | ||
} | ||
else | ||
{ | ||
return list.Count == 0; | ||
} | ||
} | ||
|
||
public static T[] ToArray<T>(this IList list) | ||
{ | ||
var array = new T[list.Count]; | ||
list.CopyTo(array, 0); | ||
return array; | ||
} | ||
} |
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
58 changes: 27 additions & 31 deletions
58
ObservableCollection/MintPlayer.ObservableCollection.Test/Person.cs
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,46 +1,42 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.ComponentModel; | ||
using System.ComponentModel; | ||
using System.Runtime.CompilerServices; | ||
using System.Text; | ||
|
||
namespace MintPlayer.ObservableCollection.Test | ||
namespace MintPlayer.ObservableCollection.Test; | ||
|
||
public class Person : INotifyPropertyChanged | ||
{ | ||
public class Person : INotifyPropertyChanged | ||
public event PropertyChangedEventHandler PropertyChanged; | ||
protected void OnPropertyChanged([CallerMemberName] string propertyName = "") | ||
{ | ||
public event PropertyChangedEventHandler PropertyChanged; | ||
protected void OnPropertyChanged([CallerMemberName] string propertyName = "") | ||
if (PropertyChanged != null) | ||
{ | ||
if (PropertyChanged != null) | ||
{ | ||
PropertyChanged(this, new PropertyChangedEventArgs(propertyName)); | ||
} | ||
PropertyChanged(this, new PropertyChangedEventArgs(propertyName)); | ||
} | ||
} | ||
|
||
private string firstname; | ||
public string FirstName | ||
private string firstname; | ||
public string FirstName | ||
{ | ||
get => firstname; | ||
set | ||
{ | ||
get { return firstname; } | ||
set | ||
{ | ||
firstname = value; | ||
OnPropertyChanged(); | ||
} | ||
firstname = value; | ||
OnPropertyChanged(); | ||
} | ||
} | ||
|
||
private string lastname; | ||
public string LastName | ||
private string lastname; | ||
public string LastName | ||
{ | ||
get => lastname; | ||
set | ||
{ | ||
get { return lastname; } | ||
set | ||
{ | ||
lastname = value; | ||
OnPropertyChanged(); | ||
} | ||
lastname = value; | ||
OnPropertyChanged(); | ||
} | ||
} | ||
|
||
public string FullName => $"{firstname} {lastname}"; | ||
public string FullName => $"{firstname} {lastname}"; | ||
|
||
public override string ToString() => FullName; | ||
} | ||
public override string ToString() => FullName; | ||
} |
224 changes: 110 additions & 114 deletions
224
ObservableCollection/MintPlayer.ObservableCollection.Test/Program.cs
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,133 +1,129 @@ | ||
using System; | ||
using System.Linq; | ||
using System.Threading; | ||
using MintPlayer.ObservableCollection.Extensions; | ||
using MintPlayer.ObservableCollection.Extensions; | ||
using MintPlayer.ObservableCollection.Test.Extensions; | ||
|
||
namespace MintPlayer.ObservableCollection.Test | ||
namespace MintPlayer.ObservableCollection.Test; | ||
|
||
static class Program | ||
{ | ||
static class Program | ||
static void Main(string[] args) | ||
{ | ||
static void Main(string[] args) | ||
{ | ||
//Demo1(); | ||
//Demo2(); | ||
//Demo3(); | ||
DemoMaxItemCount(); | ||
Console.ReadKey(); | ||
} | ||
|
||
private static void Demo1() | ||
//Demo1(); | ||
//Demo2(); | ||
//Demo3(); | ||
DemoMaxItemCount(); | ||
Console.ReadKey(); | ||
} | ||
|
||
private static void Demo1() | ||
{ | ||
var collection = new ObservableCollection<string>(); | ||
collection.CollectionChanged += (sender, e) => | ||
{ | ||
var collection = new ObservableCollection<string>(); | ||
collection.CollectionChanged += (sender, e) => | ||
Console.WriteLine($"Collection changed:"); | ||
if (!e.NewItems.IsNullOrEmpty()) | ||
{ | ||
Console.WriteLine($"Collection changed:"); | ||
if (!e.NewItems.IsNullOrEmpty()) | ||
{ | ||
var newItemsArray = new string[e.NewItems.Count]; | ||
e.NewItems.CopyTo(newItemsArray, 0); | ||
Console.WriteLine($"- items added: {string.Join(", ", newItemsArray)}"); | ||
} | ||
if (!e.OldItems.IsNullOrEmpty()) | ||
{ | ||
var oldItemsArray = new string[e.OldItems.Count]; | ||
e.OldItems.CopyTo(oldItemsArray, 0); | ||
Console.WriteLine($"- items removed: {string.Join(", ", oldItemsArray)}"); | ||
} | ||
}; | ||
|
||
collection.Add("Michael"); | ||
collection.Enabled = false; | ||
collection.Add("Junior"); | ||
collection.Enabled = true; | ||
collection.Add("Jackson"); | ||
} | ||
|
||
private static void Demo2() | ||
var newItemsArray = new string[e.NewItems.Count]; | ||
e.NewItems.CopyTo(newItemsArray, 0); | ||
Console.WriteLine($"- items added: {string.Join(", ", newItemsArray)}"); | ||
} | ||
if (!e.OldItems.IsNullOrEmpty()) | ||
{ | ||
var oldItemsArray = new string[e.OldItems.Count]; | ||
e.OldItems.CopyTo(oldItemsArray, 0); | ||
Console.WriteLine($"- items removed: {string.Join(", ", oldItemsArray)}"); | ||
} | ||
}; | ||
|
||
collection.Add("Michael"); | ||
collection.Enabled = false; | ||
collection.Add("Junior"); | ||
collection.Enabled = true; | ||
collection.Add("Jackson"); | ||
} | ||
|
||
private static void Demo2() | ||
{ | ||
var collection = new ObservableCollection<Person>(); | ||
collection.CollectionChanged += (sender, e) => | ||
{ | ||
var collection = new ObservableCollection<Person>(); | ||
collection.CollectionChanged += (sender, e) => | ||
Console.WriteLine($"Collection changed:"); | ||
if (!e.NewItems.IsNullOrEmpty()) | ||
{ | ||
Console.WriteLine($"Collection changed:"); | ||
if (!e.NewItems.IsNullOrEmpty()) | ||
{ | ||
Console.WriteLine($"- items added: {string.Join(", ", e.NewItems.ToArray<Person>().Select(p => p.FullName))}"); | ||
} | ||
if (!e.OldItems.IsNullOrEmpty()) | ||
{ | ||
Console.WriteLine($"- items removed: {string.Join(", ", e.OldItems.ToArray<Person>().Select(p => p.FullName))}"); | ||
} | ||
}; | ||
collection.ItemPropertyChanged += (sender, e) => | ||
Console.WriteLine($"- items added: {string.Join(", ", e.NewItems.ToArray<Person>().Select(p => p.FullName))}"); | ||
} | ||
if (!e.OldItems.IsNullOrEmpty()) | ||
{ | ||
Console.WriteLine($"Item property changed: {e.PropertyName}"); | ||
}; | ||
Console.WriteLine($"- items removed: {string.Join(", ", e.OldItems.ToArray<Person>().Select(p => p.FullName))}"); | ||
} | ||
}; | ||
collection.ItemPropertyChanged += (sender, e) => | ||
{ | ||
Console.WriteLine($"Item property changed: {e.PropertyName}"); | ||
}; | ||
|
||
new Thread(new ThreadStart(() => | ||
{ | ||
var person1 = new Person { FirstName = "John", LastName = "Doe" }; | ||
var person2 = new Person { FirstName = "Jimmy", LastName = "Fallon" }; | ||
var person3 = new Person { FirstName = "Michael", LastName = "Douglas" }; | ||
new Thread(new ThreadStart(() => | ||
{ | ||
var person1 = new Person { FirstName = "John", LastName = "Doe" }; | ||
var person2 = new Person { FirstName = "Jimmy", LastName = "Fallon" }; | ||
var person3 = new Person { FirstName = "Michael", LastName = "Douglas" }; | ||
|
||
// Add 3 items at once | ||
collection.AddRange([person1, person2, person3]); | ||
// Add 3 items at once | ||
collection.AddRange([person1, person2, person3]); | ||
|
||
// Change an item property | ||
collection[1].LastName = "Knibble"; | ||
// Change an item property | ||
collection[1].LastName = "Knibble"; | ||
|
||
// Replace item | ||
collection[1] = new Person { FirstName = "Sim", LastName = "Salabim" }; | ||
// Replace item | ||
collection[1] = new Person { FirstName = "Sim", LastName = "Salabim" }; | ||
|
||
// Remove 2 items at once | ||
collection.RemoveRange([person1, person3]); | ||
// Remove 2 items at once | ||
collection.RemoveRange([person1, person3]); | ||
|
||
// Add some more people | ||
var person4 = new Person { FirstName = "Johnny", LastName = "Logan" }; | ||
var person5 = new Person { FirstName = "Kiddy", LastName = "Bull" }; | ||
var person6 = new Person { FirstName = "Jacky", LastName = "Chan" }; | ||
collection.AddRange([person4, person5, person6]); | ||
// Add some more people | ||
var person4 = new Person { FirstName = "Johnny", LastName = "Logan" }; | ||
var person5 = new Person { FirstName = "Kiddy", LastName = "Bull" }; | ||
var person6 = new Person { FirstName = "Jacky", LastName = "Chan" }; | ||
collection.AddRange([person4, person5, person6]); | ||
|
||
// Remove range using index | ||
collection.RemoveRange(1, 2); | ||
})).Start(); | ||
} | ||
// Remove range using index | ||
collection.RemoveRange(1, 2); | ||
})).Start(); | ||
} | ||
|
||
private static void Demo3() | ||
{ | ||
var col = new ObservableCollection<Person>(); | ||
var people = new[] { | ||
new Person { FirstName = "Pieterjan", LastName = "De Clippel" }, | ||
new Person { FirstName = "Sam", LastName = "Hunt" }, | ||
new Person { FirstName = "Michael", LastName = "Jefferson" }, | ||
new Person { FirstName = "Bill", LastName = "Belichick" }, | ||
}; | ||
col.AddRange(people); | ||
} | ||
|
||
private static void DemoMaxItemCount() | ||
{ | ||
Console.WriteLine("MaxItemCount demo"); | ||
const int maxItemCount = 20; | ||
var col = new ObservableCollection<int>(); | ||
col.AddRange(Enumerable.Range(0, maxItemCount)); | ||
Console.WriteLine(string.Join(", ", col)); | ||
|
||
// Should remove from head | ||
col.Add(20, maxItemCount); | ||
Console.WriteLine(string.Join(", ", col)); | ||
|
||
// Should remove from tail | ||
col.Insert(1, 21, maxItemCount); | ||
Console.WriteLine(string.Join(", ", col)); | ||
|
||
// Should remove from head | ||
col.Insert(maxItemCount / 2, 22, maxItemCount); | ||
Console.WriteLine(string.Join(", ", col)); | ||
|
||
// Should remove from tail | ||
col.Insert(maxItemCount / 2 - 1, 23, maxItemCount); | ||
Console.WriteLine(string.Join(", ", col)); | ||
} | ||
private static void Demo3() | ||
{ | ||
var col = new ObservableCollection<Person>(); | ||
var people = new[] { | ||
new Person { FirstName = "Pieterjan", LastName = "De Clippel" }, | ||
new Person { FirstName = "Sam", LastName = "Hunt" }, | ||
new Person { FirstName = "Michael", LastName = "Jefferson" }, | ||
new Person { FirstName = "Bill", LastName = "Belichick" }, | ||
}; | ||
col.AddRange(people); | ||
} | ||
|
||
private static void DemoMaxItemCount() | ||
{ | ||
Console.WriteLine("MaxItemCount demo"); | ||
const int maxItemCount = 20; | ||
var col = new ObservableCollection<int>(); | ||
col.AddRange(Enumerable.Range(0, maxItemCount)); | ||
Console.WriteLine(string.Join(", ", col)); | ||
|
||
// Should remove from head | ||
col.Add(20, maxItemCount); | ||
Console.WriteLine(string.Join(", ", col)); | ||
|
||
// Should remove from tail | ||
col.Insert(1, 21, maxItemCount); | ||
Console.WriteLine(string.Join(", ", col)); | ||
|
||
// Should remove from head | ||
col.Insert(maxItemCount / 2, 22, maxItemCount); | ||
Console.WriteLine(string.Join(", ", col)); | ||
|
||
// Should remove from tail | ||
col.Insert(maxItemCount / 2 - 1, 23, maxItemCount); | ||
Console.WriteLine(string.Join(", ", col)); | ||
} | ||
} |
Oops, something went wrong.