using System; using System.Linq; using System.Collections; using System.Collections.Generic; using System.Collections.Concurrent; using System.Threading.Tasks; using NUnit.Framework; namespace Common.Test { public class Test { [Test] public async Task AsyncFor() { for (int i = 0; i < 5; i++) { await Task.Delay(1000); TestContext.WriteLine($"{i} done at {DateTimeOffset.UtcNow}"); } } [Test] public void ConcurrentCollection() { List list = new List(); for (int i = 0; i < 1000; i++) { Task.Run(() => { list.Add(i); }); } TestContext.WriteLine($"{list.Count}"); ConcurrentQueue queue = new ConcurrentQueue(); for (int i = 0; i < 1000; i++) { Task.Run(() => { queue.Enqueue(i); }); } TestContext.WriteLine($"{queue.Count}"); } [Test] public void ObjectType() { List list = new List { 1, "hello", 2, "world" }; TestContext.WriteLine(list is IList); object[] objs = { 1, "hi", 3 }; TestContext.WriteLine(objs is IList); List subList = list.OfType().ToList(); foreach (object obj in subList) { TestContext.WriteLine(obj); } } [Test] public void CollectionExcept() { List list1 = new List { 1, 2, 3, 4, 5 }; List list2 = new List { 4, 5, 6 }; IEnumerable deltaList = list1.Except(list2).ToList(); foreach (int delta in deltaList) { TestContext.WriteLine(delta); } Dictionary dict1 = new Dictionary { { "a", 1 }, { "b", 2 } }; Dictionary dict2 = new Dictionary { { "b", 2 }, { "c", 3 } }; IEnumerable> deltaDict = dict1.Except(dict2); foreach (KeyValuePair delta in deltaDict) { TestContext.WriteLine($"{delta.Key} : {delta.Value}"); } } [Test] public void Union() { Dictionary dict1 = new Dictionary { { "a", 1 }, { "b", 2 }, { "c", 3 } }; Dictionary dict2 = new Dictionary { { "b", "b" }, { "c", "c" }, { "d", "d" } }; IEnumerable keys = dict1.Keys.Union(dict2.Keys); foreach (string key in keys) { TestContext.WriteLine(key); } } } }