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)); }
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 }; Assert.IsTrue(dict.Contains(dict.First())); Assert.IsTrue(dict.Contains(dict.First(), null)); Assert.IsTrue(sortedDict.Contains(sortedDict.First())); Assert.IsTrue(sortedDict.Contains(sortedDict.First(), null)); }
/// <summary> /// The mode of the set of lengths of lines in a string. Whatever /// number occurs the most as length of characters in a line is returned. /// </summary> /// <returns>The line length that occurs the most.</returns> /// <param name="input">Input.</param> private int LengthMode(string input) { string[] lines = input.Split('\n'); var modes = new System.Collections.Generic.Dictionary <int, int>(); foreach (var line in lines) { if (modes.ContainsKey(line.Length)) { modes[line.Length] += 1; } else { modes[line.Length] = 1; } } return(modes.Aggregate(modes.First(), (a, b) => { if (b.Value > a.Value) { return b; } else { return a; } }).Key); }
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); } }
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 }; Assert.IsTrue(dict.SequenceEqual(dict.DefaultIfEmpty())); Assert.IsTrue(dict.SequenceEqual(dict.DefaultIfEmpty(dict.First()))); Assert.IsTrue(sortedDict.SequenceEqual(sortedDict.DefaultIfEmpty())); Assert.IsTrue(sortedDict.SequenceEqual(sortedDict.DefaultIfEmpty(sortedDict.First()))); }
private string GetFirstNoHeaderNode(System.Collections.Generic.Dictionary <string, NavItem> navItems, string id, out bool isAllCheck, out string nodeKey) { nodeKey = string.Empty; isAllCheck = true; System.Text.StringBuilder stringBuilder = new System.Text.StringBuilder(); stringBuilder.Append("<div class=\"two fl clearfix\">"); if (navItems.Values.Count > 0) { System.Collections.Generic.KeyValuePair <string, NavItem> keyValuePair = navItems.First <System.Collections.Generic.KeyValuePair <string, NavItem> >(); if (string.IsNullOrEmpty(keyValuePair.Value.SpanName)) { nodeKey = keyValuePair.Key; int num = 0; using (System.Collections.Generic.Dictionary <string, NavPageLink> .Enumerator enumerator = keyValuePair.Value.PageLinks.GetEnumerator()) { while (enumerator.MoveNext()) { System.Collections.Generic.KeyValuePair <string, NavPageLink> current = enumerator.Current; string permissionId = RolePermissionInfo.GetPermissionId(id, current.Value.ID); bool flag = this.oldPermissions.FirstOrDefault((RolePermissionInfo p) => p.PermissionId == permissionId) != null; if (!flag) { isAllCheck = false; } if (num == 0) { stringBuilder.Append(" <div class=\"titlecheck fl\">"); stringBuilder.AppendFormat(" <label class=\"setalign\"> <input type=\"checkbox\" name=\"permissions\" value=\"{0}\" {1}>{2}</label>", permissionId, this.GetInputCheck(flag), current.Value.Title); stringBuilder.Append("</div>"); } else { stringBuilder.Append(" <div class=\"twoinerlist fl\">"); stringBuilder.AppendFormat(" <label class=\"setalign\"> <input type=\"checkbox\" name=\"permissions\" value=\"{0}\" {1}>{2}</label>", permissionId, this.GetInputCheck(flag), current.Value.Title); stringBuilder.Append("</div>"); } num++; } goto IL_185; } } stringBuilder.Append(" <div class=\"titlecheck fl\">"); stringBuilder.Append(" "); stringBuilder.Append("</div>"); } IL_185: stringBuilder.Append("</div>"); return(stringBuilder.ToString()); }