Example #1
0
        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;
                break;
            }

            System.Collections.Generic.KeyValuePair <int, int> sortedDictKvp = default(System.Collections.Generic.KeyValuePair <int, int>);
            foreach (var kv in sortedDict)
            {
                sortedDictKvp = kv;
                break;
            }

            Assert.AreEqual(dictKvp, dict.First());
            Assert.AreEqual(dictKvp, dict.First(x => true));
            Assert.AreEqual(dictKvp, dict.FirstOrDefault());
            Assert.AreEqual(dictKvp, dict.FirstOrDefault(x => true));
            Assert.AreEqual(sortedDictKvp, sortedDict.First());
            Assert.AreEqual(sortedDictKvp, sortedDict.First(x => true));
            Assert.AreEqual(sortedDictKvp, sortedDict.FirstOrDefault());
            Assert.AreEqual(sortedDictKvp, sortedDict.FirstOrDefault(x => true));
        }
Example #2
0
        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.First(null); Assert.Fail(); }catch (ArgumentNullException exc) { Assert.AreEqual("predicate", exc.ParamName); }
            try { dict.FirstOrDefault(null); Assert.Fail(); } catch (ArgumentNullException exc) { Assert.AreEqual("predicate", exc.ParamName); }
            try { sortedDict.First(null); Assert.Fail(); } catch (ArgumentNullException exc) { Assert.AreEqual("predicate", exc.ParamName); }
            try { sortedDict.FirstOrDefault(null); Assert.Fail(); } catch (ArgumentNullException exc) { Assert.AreEqual("predicate", exc.ParamName); }
        }