public void ShouldReturnCounterNullForClear() { var d = new Dictionary<string, int>(); d.Add("cats", 12); d.Add("dogs", 133); d.Clear(); Assert.Equal(0, d.Count); }
static void Main(string[] args) { var namesAndAges = new Dictionary<string, int>/*(StringComparer.CurrentCultureIgnoreCase)*/ { {"Jeremy", 35} }; namesAndAges.Add("Josh", 25); namesAndAges["Kristin"] = 39; //namesAndAges.Add("Jeremy", 30); // an exception namesAndAges["Jeremy"] = 30; if (namesAndAges.ContainsKey("Caleb")) { var foo = namesAndAges["Caleb"]; // } foreach (var kvp in namesAndAges) { //kvp.Key //kvp.Value } foreach (var key in namesAndAges.Keys) { } foreach (var value in namesAndAges.Values) { } var count = namesAndAges.Count; if (namesAndAges.ContainsKey("Josh")) { namesAndAges.Remove("Josh"); } namesAndAges.Clear(); Console.ReadLine(); }
static void Main(string[] args) { Dictionary<int, Person> dic = new Dictionary<int, Person>(); Dictionary<string, Person> dic2 = new Dictionary<string, Person>(); Person p1 = new Person("Frey"); Person p2 = new Person("Leon"); dic.Add(p1.PersonId, p1); dic.Add(p2.PersonId, p2); foreach (var item in dic) { Console.WriteLine(item.Key + " " + item.Value.Name); } dic2.Add("Frey", new Person()); dic2.Add("Leon", new Person()); foreach (var item in dic2) { Console.WriteLine(item.Key + " " + item.Value.PersonId); } if (dic2.ContainsKey("Leon")) { Console.WriteLine("Key Leon Exists"); } Console.WriteLine("Delete Dictionary"); dic.Clear(); Console.WriteLine(); Console.WriteLine(""); foreach (var item in dic) { Console.WriteLine(item.Key + " " + item.Value.Name); } }
static void Main(string[] args) { Dictionary<char, string> certifications = new Dictionary<char, string>(); certifications.Add('P', "Passenger transport"); certifications.Add('H', "Hazardous materials"); certifications.Add('N', "Tank vehicles (liquids in bulk)"); certifications.Add('T', "Double/Triple trailers"); ShowCertifications(certifications); //search for certfications by key and confirm string theValue; //try get value //uses the out certifications.TryGetValue('H', out theValue); Console.WriteLine(theValue); if (certifications.ContainsKey('P')) Console.WriteLine("Has certfication 'P'"); //check certification for a specific value and confirm if (certifications.ContainsValue("Double/Triple trailers")) Console.WriteLine("Has certification 'T'"); //remove certification 'T' by key. certifications.Remove('T'); if (!certifications.ContainsValue("Double/Triple trailers")) Console.WriteLine("No longer has certification 'T'"); //remove all certifications certifications.Clear(); if (certifications.Count == 0) Console.WriteLine("All certifications removed"); Console.ReadLine(); }
public static void Main(string[] args) { Dictionary<string,int> dict=new Dictionary<string,int>(); dict.Add("one",1); dict.Add("two",2); dict.Add("three",3); dict.Add("four",4); dict.Remove("one"); Console.WriteLine ("Before contains"); Console.WriteLine(dict.ContainsKey("dsaf")); Console.WriteLine("Key Value Pairs after Dictionary related operations:\n"); foreach (var pair in dict) { Console.WriteLine("{0}, {1}", pair.Key, pair.Value); } dict.Clear (); List<string> strList = new List<string> (); strList.Add ("one"); strList.Add ("two"); strList.Add ("three"); strList.Add ("four"); strList.Add ("five"); strList.Insert (3, "great"); string[] newList = new string[3]{ "ten", "eleven", "twelve" }; strList.AddRange (newList); strList.InsertRange (3, newList); Console.WriteLine ("Output after all list related operations i.e. add, insert, addrange, insertrange,remove\n"); Console.WriteLine ("The list"); foreach (var i in strList) Console.WriteLine (i); }
public void ChecksIfClearFunctionWorks() { var data = new Dictionary<string, int> { { "first", 2 }, { "second", 3 } }; data.Clear(); Assert.Equal(0, data.Count); }