public async Task TestTryGetValue() { var csvData = "A;a" + Environment.NewLine + "B;b" + Environment.NewLine + "C;c"; using var csvReader = new CsvReader(csvData); var csvDictionary = new CsvDictionary(); Assert.NotNull(csvDictionary); await csvDictionary.LoadAsync(csvReader); Assert.True(csvDictionary.TryGetValue("A", out string v1)); Assert.Equal("a", v1); Assert.True(csvDictionary.TryGetValue("B", out string v2)); Assert.Equal("b", v2); Assert.True(csvDictionary.TryGetValue("C", out string v3)); Assert.Equal("c", v3); Assert.False(csvDictionary.TryGetValue("D", out string v4)); Assert.Null(v4); }
static void Main(string[] args) { TestData td0 = new TestData() { Id = "1234567890", Name = "DESKTOP-123", Age = 22, HappyDay = DateTime.Now, BytesArray = new byte[] { 0x11, 0x12, 0x13, 0x14 }, IgnoreData = 12345, StringArray = new List <string>() }; TestData td1 = new TestData() { Id = "0987654321", Name = "DESKTOP-456", Age = 23, HappyDay = DateTime.Now, BytesArray = new byte[] { 0x2A, 0x2B, 0x2E, 0x2F }, IgnoreData = 12345, StringArray = new List <string>() }; td0.StringArray.Add("abc"); CsvDictionary <String> csvd = new CsvDictionary <String>(); csvd.IsAutoSave = true; csvd.Add(td0); if (csvd.IsAutoSave) { Console.WriteLine("Sleep.."); int i = 0; while (i < 100000) { i++; Thread.SpinWait(i); } } td0.StringArray.Add("def"); td0.StringArray.Add("wxv"); csvd.Replace(td0); if (csvd.IsAutoSave) { Console.WriteLine("Sleep.."); int i = 0; while (i < 100000) { i++; Thread.SpinWait(i); } } csvd.AddOrUpdate(td1); if (!csvd.IsAutoSave) { //csvd.Save(); //csvd.Load<TestData>(); /// or csvd.FlushAndReload(); } TestData tdItem = null; bool ret = csvd.TryGetValue <TestData>("1234567890", out tdItem); tdItem.StringArray.Add("fgh"); tdItem.StringArray.Add("zzz"); tdItem.StringArray.Add("qqq"); if (csvd.IsAutoSave) { Console.WriteLine("Sleep.."); int i = 0; while (i < 100000) { i++; Thread.SpinWait(i); } } td1.StringArray.Add("end1"); td1.StringArray.Add("end2"); csvd.AddOrUpdate(td1); if (csvd.IsAutoSave) { Console.WriteLine("Sleep.."); int i = 0; while (i < 100000) { i++; Thread.SpinWait(i); } } Console.WriteLine( "End -> return {0}: {1}", ret, ((tdItem == null) ? "item not found" : tdItem.ToString()) ); if (!csvd.IsAutoSave) { Console.ReadLine(); } }