public void Chaining_Dictionary() { var dict = new System.Collections.Generic.Dictionary <int, int> { [1] = 2, [3] = 4 }; var sortedDict = new System.Collections.Generic.SortedDictionary <int, int> { [1] = 2, [3] = 4 }; System.Collections.Generic.KeyValuePair <int, int> dictKvp = default(System.Collections.Generic.KeyValuePair <int, int>); foreach (var kv in dict) { dictKvp = kv; } System.Collections.Generic.KeyValuePair <int, int> sortedDictKvp = default(System.Collections.Generic.KeyValuePair <int, int>); foreach (var kv in sortedDict) { sortedDictKvp = kv; } Assert.AreEqual(dictKvp, dict.Last()); Assert.AreEqual(dictKvp, dict.Last(x => true)); Assert.AreEqual(dictKvp, dict.LastOrDefault()); Assert.AreEqual(dictKvp, dict.LastOrDefault(x => true)); Assert.AreEqual(sortedDictKvp, sortedDict.Last()); Assert.AreEqual(sortedDictKvp, sortedDict.Last(x => true)); Assert.AreEqual(sortedDictKvp, sortedDict.LastOrDefault()); Assert.AreEqual(sortedDictKvp, sortedDict.LastOrDefault(x => true)); }
public void Errors_Dictionary() { var dict = new System.Collections.Generic.Dictionary <int, int> { [1] = 2, [3] = 4 }; var sortedDict = new System.Collections.Generic.SortedDictionary <int, int> { [1] = 2, [3] = 4 }; try { dict.Last(null); Assert.Fail(); } catch (ArgumentNullException exc) { Assert.AreEqual("predicate", exc.ParamName); } try { dict.LastOrDefault(null); Assert.Fail(); } catch (ArgumentNullException exc) { Assert.AreEqual("predicate", exc.ParamName); } try { sortedDict.Last(null); Assert.Fail(); } catch (ArgumentNullException exc) { Assert.AreEqual("predicate", exc.ParamName); } try { sortedDict.LastOrDefault(null); Assert.Fail(); } catch (ArgumentNullException exc) { Assert.AreEqual("predicate", exc.ParamName); } }