static void Main(string[] args) { System.Collections.Specialized.StringDictionary stringDictionary = new System.Collections.Specialized.StringDictionary(); stringDictionary.Add("Test1", "Test@123"); stringDictionary.Add("Admin", "Admin@123"); stringDictionary.Add("Temp", "Temp@123"); stringDictionary.Add("Demo", "Demo@123"); stringDictionary.Add("Test2", "Test2@123"); stringDictionary.Remove("Admin"); if (stringDictionary.ContainsKey("Admin")) { Console.WriteLine("UserName already Esists"); } else { stringDictionary.Add("Admin", "Admin@123"); Console.WriteLine("User added succesfully."); } // Get a collection of the keys. Console.WriteLine("UserName" + ": " + "Password"); foreach (DictionaryEntry entry in stringDictionary) { Console.WriteLine(entry.Key + ": " + entry.Value); } Console.ReadKey(); }
public Args(string[] args) { argDict = new StringDictionary(); Regex regEx = new Regex(@"^-", RegexOptions.IgnoreCase | RegexOptions.Compiled); Regex regTrim = new Regex(@"^['""]?(.*?)['""]?$", RegexOptions.IgnoreCase | RegexOptions.Compiled); string arg = ""; string[] chunks; foreach (string s in args) { chunks = regEx.Split(s, 3); if (regEx.IsMatch(s)) { arg = chunks[1]; argDict.Add(arg, "true"); } else { if (argDict.ContainsKey(arg)) { chunks[0] = regTrim.Replace(chunks[0], "$1"); argDict.Remove(arg); argDict.Add(arg, chunks[0]); arg = ""; } } } }
public void MergeTo (StringDictionary vars) { foreach (KeyValuePair<string,string> ev in variables) { if (ev.Value == null) vars.Remove (ev.Key); else vars [ev.Key] = ev.Value; } }
public static void Main() { // Creates and initializes a new StringDictionary. StringDictionary myCol = new StringDictionary(); myCol.Add("red", "rojo"); myCol.Add("green", "verde"); myCol.Add("blue", "azul"); Console.WriteLine("Count: {0}", myCol.Count); // Display the contents of the collection using foreach. This is the preferred method. Console.WriteLine("Displays the elements using foreach:"); PrintKeysAndValues1(myCol); // Display the contents of the collection using the enumerator. Console.WriteLine("Displays the elements using the IEnumerator:"); PrintKeysAndValues2(myCol); // Display the contents of the collection using the Keys, Values, Count, and Item properties. Console.WriteLine("Displays the elements using the Keys, Values, Count, and Item properties:"); PrintKeysAndValues3(myCol); // Copies the StringDictionary to an array with DictionaryEntry elements. DictionaryEntry[] myArr = new DictionaryEntry[myCol.Count]; myCol.CopyTo(myArr, 0); // Displays the values in the array. Console.WriteLine("Displays the elements in the array:"); Console.WriteLine(" KEY VALUE"); for (int i = 0; i < myArr.Length; i++) Console.WriteLine(" {0,-10} {1}", myArr[i].Key, myArr[i].Value); Console.WriteLine(); // Searches for a value. if (myCol.ContainsValue("amarillo")) Console.WriteLine("The collection contains the value \"amarillo\"."); else Console.WriteLine("The collection does not contain the value \"amarillo\"."); Console.WriteLine(); // Searches for a key and deletes it. if (myCol.ContainsKey("green")) myCol.Remove("green"); Console.WriteLine("The collection contains the following elements after removing \"green\":"); PrintKeysAndValues1(myCol); // Clears the entire collection. myCol.Clear(); Console.WriteLine("The collection contains the following elements after it is cleared:"); PrintKeysAndValues1(myCol); }
public void Empty () { StringDictionary sd = new StringDictionary (); Assert.AreEqual (0, sd.Count, "Count"); Assert.IsFalse (sd.IsSynchronized, "IsSynchronized"); Assert.AreEqual (0, sd.Keys.Count, "Keys"); Assert.AreEqual (0, sd.Values.Count, "Values"); Assert.IsNotNull (sd.SyncRoot, "SyncRoot"); Assert.IsFalse (sd.ContainsKey ("a"), "ContainsKey"); Assert.IsFalse (sd.ContainsValue ("1"), "ContainsValue"); sd.CopyTo (new DictionaryEntry[0], 0); Assert.IsNotNull (sd.GetEnumerator (), "GetEnumerator"); sd.Remove ("a"); // doesn't exists sd.Clear (); }
public InstallContext (string logFilePath, string [] cmdLine) { parameters = ParseCommandLine(cmdLine); // Log file path specified in command line arguments // has higher priority than the logFilePath argument if (parameters.ContainsKey ("logFile")) { logFilePath = parameters ["logFile"]; parameters.Remove ("logFile"); } if (logFilePath == null) logFilePath = ""; parameters.Add ("logFile", logFilePath); }
public void Test01() { IntlStrings intl; StringDictionary sd; // simple string values string[] values = { "", " ", "a", "aa", "tExt", " spAces", "1", "$%^#", "2222222222222222222222222", System.DateTime.Today.ToString(), Int32.MaxValue.ToString() }; // keys for simple string values string[] keys = { "zero", "one", " ", "", "aa", "1", System.DateTime.Today.ToString(), "$%^#", Int32.MaxValue.ToString(), " spaces", "2222222222222222222222222" }; Array arr; ICollection ks; // Keys collection int ind; // initialize IntStrings intl = new IntlStrings(); // [] StringDictionary is constructed as expected //----------------------------------------------------------------- sd = new StringDictionary(); // [] get Keys on empty dictionary // if (sd.Count > 0) sd.Clear(); if (sd.Keys.Count != 0) { Assert.False(true, string.Format("Error, returned Keys.Count = {0}", sd.Keys.Count)); } // // [] get Keys on filled dictionary // int len = values.Length; sd.Clear(); for (int i = 0; i < len; i++) { sd.Add(keys[i], values[i]); } if (sd.Count != len) { Assert.False(true, string.Format("Error, count is {0} instead of {1}", sd.Count, len)); } ks = sd.Keys; if (ks.Count != len) { Assert.False(true, string.Format("Error, returned Keys.Count = {0}", ks.Count)); } arr = Array.CreateInstance(typeof(string), len); ks.CopyTo(arr, 0); for (int i = 0; i < len; i++) { ind = Array.IndexOf(arr, keys[i].ToLowerInvariant()); if (ind < 0) { Assert.False(true, string.Format("Error, Keys doesn't contain \"{1}\" key. Search result: {2}", i, keys[i], ind)); } } // // [] get Keys on dictionary with identical values // sd.Clear(); string intlStr = intl.GetRandomString(MAX_LEN); sd.Add("keykey1", intlStr); // 1st duplicate for (int i = 0; i < len; i++) { sd.Add(keys[i], values[i]); } sd.Add("keykey2", intlStr); // 2nd duplicate if (sd.Count != len + 2) { Assert.False(true, string.Format("Error, count is {0} instead of {1}", sd.Count, len + 2)); } // get Keys // ks = sd.Keys; if (ks.Count != sd.Count) { Assert.False(true, string.Format("Error, returned Keys.Count = {0}", ks.Count)); } arr = Array.CreateInstance(typeof(string), len + 2); ks.CopyTo(arr, 0); for (int i = 0; i < len; i++) { ind = Array.IndexOf(arr, keys[i].ToLowerInvariant()); if (ind < 0) { Assert.False(true, string.Format("Error, Keys doesn't contain \"{1}\" key", i, keys[i])); } } ind = Array.IndexOf(arr, "keykey1"); if (ind < 0) { Assert.False(true, string.Format("Error, Keys doesn't contain {0} key", "keykey1")); } ind = Array.IndexOf(arr, "keykey2"); if (ind < 0) { Assert.False(true, string.Format("Error, Keys doesn't contain \"{0}\" key", "keykey2")); } // // Intl strings // [] get Keys on dictionary filled with Intl strings // string[] intlValues = new string[len * 2]; // fill array with unique strings // for (int i = 0; i < len * 2; i++) { string val = intl.GetRandomString(MAX_LEN); while (Array.IndexOf(intlValues, val) != -1) val = intl.GetRandomString(MAX_LEN); intlValues[i] = val; } // // will use first half of array as values and second half as keys // sd.Clear(); for (int i = 0; i < len; i++) { sd.Add(intlValues[i + len], intlValues[i]); } if (sd.Count != len) { Assert.False(true, string.Format("Error, count is {0} instead of {1}", sd.Count, len)); } ks = sd.Keys; if (ks.Count != len) { Assert.False(true, string.Format("Error, returned Keys.Count = {0}", ks.Count)); } arr = Array.CreateInstance(typeof(string), len); ks.CopyTo(arr, 0); for (int i = 0; i < arr.Length; i++) { ind = Array.IndexOf(arr, intlValues[i + len].ToLowerInvariant()); if (ind < 0) { Assert.False(true, string.Format("Error, Keys doesn't contain \"{1}\" key", i, intlValues[i + len])); } } // // Case sensitivity: keys are always lowercased - not doing it // [] Change dictionary and check Keys // sd.Clear(); for (int i = 0; i < len; i++) { sd.Add(keys[i], values[i]); } if (sd.Count != len) { Assert.False(true, string.Format("Error, count is {0} instead of {1}", sd.Count, len)); } ks = sd.Keys; if (ks.Count != len) { Assert.False(true, string.Format("Error, returned Keys.Count = {0}", ks.Count)); } sd.Remove(keys[0]); if (sd.Count != len - 1) { Assert.False(true, string.Format("Error, didn't remove element")); } if (ks.Count != len - 1) { Assert.False(true, string.Format("Error, Keys were not updated after removal")); } arr = Array.CreateInstance(typeof(string), sd.Count); ks.CopyTo(arr, 0); ind = Array.IndexOf(arr, keys[0].ToLowerInvariant()); if (ind >= 0) { Assert.False(true, string.Format("Error, Keys still contains removed key " + ind)); } sd.Add(keys[0], "new item"); if (sd.Count != len) { Assert.False(true, string.Format("Error, didn't add element")); } if (ks.Count != len) { Assert.False(true, string.Format("Error, Keys were not updated after addition")); } arr = Array.CreateInstance(typeof(string), sd.Count); ks.CopyTo(arr, 0); ind = Array.IndexOf(arr, keys[0].ToLowerInvariant()); if (ind < 0) { Assert.False(true, string.Format("Error, Keys doesn't contain added key ")); } }
public void Test01() { StringDictionary sd; IEnumerator en; DictionaryEntry curr; // Enumerator.Current value // simple string values string[] values = { "a", "aa", "", " ", "text", " spaces", "1", "$%^#", "2222222222222222222222222", System.DateTime.Today.ToString(), Int32.MaxValue.ToString() }; // keys for simple string values string[] keys = { "zero", "one", " ", "", "aa", "1", System.DateTime.Today.ToString(), "$%^#", Int32.MaxValue.ToString(), " spaces", "2222222222222222222222222" }; // [] StringDictionary GetEnumerator() //----------------------------------------------------------------- sd = new StringDictionary(); // [] Enumerator for empty dictionary // en = sd.GetEnumerator(); string type = en.GetType().ToString(); if (type.IndexOf("Enumerator", 0) == 0) { Assert.False(true, string.Format("Error, type is not Enumerator")); } // // MoveNext should return false // bool res = en.MoveNext(); if (res) { Assert.False(true, string.Format("Error, MoveNext returned true")); } // // Attempt to get Current should result in exception // Assert.Throws<InvalidOperationException>(() => { curr = (DictionaryEntry)en.Current; }); // // Filled collection // [] Enumerator for filled dictionary // for (int i = 0; i < values.Length; i++) { sd.Add(keys[i], values[i]); } en = sd.GetEnumerator(); type = en.GetType().ToString(); if (type.IndexOf("Enumerator", 0) == 0) { Assert.False(true, string.Format("Error, type is not Enumerator")); } // // MoveNext should return true // for (int i = 0; i < sd.Count; i++) { res = en.MoveNext(); if (!res) { Assert.False(true, string.Format("Error, MoveNext returned false", i)); } curr = (DictionaryEntry)en.Current; // //enumerator enumerates in different than added order // so we'll check Contains // if (!sd.ContainsValue(curr.Value.ToString())) { Assert.False(true, string.Format("Error, Current dictionary doesn't contain value from enumerator", i)); } if (!sd.ContainsKey(curr.Key.ToString())) { Assert.False(true, string.Format("Error, Current dictionary doesn't contain key from enumerator", i)); } if (String.Compare(sd[curr.Key.ToString()], curr.Value.ToString()) != 0) { Assert.False(true, string.Format("Error, Value for current Key is different in dictionary", i)); } // while we didn't MoveNext, Current should return the same value DictionaryEntry curr1 = (DictionaryEntry)en.Current; if (!curr.Equals(curr1)) { Assert.False(true, string.Format("Error, second call of Current returned different result", i)); } } // next MoveNext should bring us outside of the collection // res = en.MoveNext(); res = en.MoveNext(); if (res) { Assert.False(true, string.Format("Error, MoveNext returned true")); } // // Attempt to get Current should result in exception // Assert.Throws<InvalidOperationException>(() => { curr = (DictionaryEntry)en.Current; }); en.Reset(); // // Attempt to get Current should result in exception // Assert.Throws<InvalidOperationException>(() => { curr = (DictionaryEntry)en.Current; }); // // [] Modify dictionary when enumerating // if (sd.Count < 1) { for (int i = 0; i < values.Length; i++) { sd.Add(keys[i], values[i]); } } en = sd.GetEnumerator(); res = en.MoveNext(); if (!res) { Assert.False(true, string.Format("Error, MoveNext returned false")); } curr = (DictionaryEntry)en.Current; int cnt = sd.Count; sd.Remove(keys[0]); if (sd.Count != cnt - 1) { Assert.False(true, string.Format("Error, didn't remove item with 0th key")); } // will return just removed item DictionaryEntry curr2 = (DictionaryEntry)en.Current; if (!curr.Equals(curr2)) { Assert.False(true, string.Format("Error, current returned different value after modification")); } // exception expected Assert.Throws<InvalidOperationException>(() => { res = en.MoveNext(); }); // // [] Modify dictionary when enumerated beyond the end // sd.Clear(); for (int i = 0; i < values.Length; i++) { sd.Add(keys[i], values[i]); } en = sd.GetEnumerator(); for (int i = 0; i < sd.Count; i++) { en.MoveNext(); } curr = (DictionaryEntry)en.Current; curr = (DictionaryEntry)en.Current; cnt = sd.Count; sd.Remove(keys[0]); if (sd.Count != cnt - 1) { Assert.False(true, string.Format("Error, didn't remove item with 0th key")); } // will return just removed item curr2 = (DictionaryEntry)en.Current; if (!curr.Equals(curr2)) { Assert.False(true, string.Format("Error, current returned different value after modification")); } // exception expected Assert.Throws<InvalidOperationException>(() => { res = en.MoveNext(); }); }
public void Test01() { IntlStrings intl; StringDictionary sd; // simple string values string[] values = { "", " ", "a", "aa", "text", " spaces", "1", "$%^#", "2222222222222222222222222", System.DateTime.Today.ToString(), Int32.MaxValue.ToString() }; // keys for simple string values string[] keys = { "zero", "one", " ", "", "aa", "1", System.DateTime.Today.ToString(), "$%^#", Int32.MaxValue.ToString(), " spaces", "2222222222222222222222222" }; int cnt = 0; // initialize IntStrings intl = new IntlStrings(); // [] StringDictionary is constructed as expected //----------------------------------------------------------------- sd = new StringDictionary(); // [] Remove() from empty dictionary // if (sd.Count > 0) sd.Clear(); for (int i = 0; i < keys.Length; i++) { sd.Remove(keys[0]); } // [] Remove() from filled dictionary // int len = values.Length; sd.Clear(); for (int i = 0; i < len; i++) { sd.Add(keys[i], values[i]); } if (sd.Count != len) { Assert.False(true, string.Format("Error, count is {0} instead of {1}", sd.Count, len)); } for (int i = 0; i < len; i++) { cnt = sd.Count; sd.Remove(keys[i]); if (sd.Count != cnt - 1) { Assert.False(true, string.Format("Error, didn't remove element with {0} key", i)); } // verify that indeed element with given key was removed if (sd.ContainsValue(values[i])) { Assert.False(true, string.Format("Error, removed wrong value", i)); } if (sd.ContainsKey(keys[i])) { Assert.False(true, string.Format("Error, removed wrong value", i)); } } // // [] Remove() on dictionary with identical values // sd.Clear(); string intlStr = intl.GetRandomString(MAX_LEN); sd.Add("keykey1", intlStr); // 1st duplicate for (int i = 0; i < len; i++) { sd.Add(keys[i], values[i]); } sd.Add("keykey2", intlStr); // 2nd duplicate if (sd.Count != len + 2) { Assert.False(true, string.Format("Error, count is {0} instead of {1}", sd.Count, len + 2)); } // remove // sd.Remove("keykey2"); if (!sd.ContainsValue(intlStr)) { Assert.False(true, string.Format("Error, removed both duplicates")); } // second string should still be present if (sd.ContainsKey("keykey2")) { Assert.False(true, string.Format("Error, removed not given instance")); } if (!sd.ContainsKey("keykey1")) { Assert.False(true, string.Format("Error, removed wrong instance")); } // // Intl strings // [] Remove() from dictionary filled with Intl strings // string[] intlValues = new string[len * 2]; // fill array with unique strings // for (int i = 0; i < len * 2; i++) { string val = intl.GetRandomString(MAX_LEN); while (Array.IndexOf(intlValues, val) != -1) val = intl.GetRandomString(MAX_LEN); intlValues[i] = val; } // // will use first half of array as values and second half as keys // sd.Clear(); for (int i = 0; i < len; i++) { sd.Add(intlValues[i + len], intlValues[i]); } if (sd.Count != len) { Assert.False(true, string.Format("Error, count is {0} instead of {1}", sd.Count, len)); } // remove for (int i = 0; i < len; i++) { cnt = sd.Count; sd.Remove(intlValues[i + len]); if (sd.Count != cnt - 1) { Assert.False(true, string.Format("Error, didn't remove element with {0} key", i + len)); } // verify that indeed element with given key was removed if (sd.ContainsValue(intlValues[i])) { Assert.False(true, string.Format("Error, removed wrong value", i)); } if (sd.ContainsKey(intlValues[i + len])) { Assert.False(true, string.Format("Error, removed wrong key", i)); } } // // [] Case sensitivity: keys are always lowercased // sd.Clear(); string[] intlValuesUpper = new string[len]; // fill array with unique strings // for (int i = 0; i < len * 2; i++) { intlValues[i] = intlValues[i].ToLowerInvariant(); } // array of uppercase keys for (int i = 0; i < len; i++) { intlValuesUpper[i] = intlValues[i + len].ToUpperInvariant(); } sd.Clear(); // // will use first half of array as values and second half as keys // for (int i = 0; i < len; i++) { sd.Add(intlValues[i + len], intlValues[i]); // adding lowercase strings } if (sd.Count != len) { Assert.False(true, string.Format("Error, count is {0} instead of {1}", sd.Count, len)); } // remove for (int i = 0; i < len; i++) { cnt = sd.Count; sd.Remove(intlValuesUpper[i]); if (sd.Count != cnt - 1) { Assert.False(true, string.Format("Error, didn't remove element with {0} lower key", i + len)); } // verify that indeed element with given key was removed if (sd.ContainsValue(intlValues[i])) { Assert.False(true, string.Format("Error, removed wrong value", i)); } if (sd.ContainsKey(intlValuesUpper[i])) { Assert.False(true, string.Format("Error, removed wrong key", i)); } } // // [] Invalid parameter // Assert.Throws<ArgumentNullException>(() => { sd.Remove(null); }); }
public void SomeElements () { StringDictionary sd = new StringDictionary (); for (int i = 0; i < 10; i++) sd.Add (i.ToString (), (i * 10).ToString ()); Assert.AreEqual ("10", sd["1"], "this[1]"); Assert.AreEqual (10, sd.Count, "Count-10"); Assert.AreEqual (10, sd.Keys.Count, "Keys"); Assert.AreEqual (10, sd.Values.Count, "Values"); Assert.IsTrue (sd.ContainsKey ("2"), "ContainsKey"); Assert.IsTrue (sd.ContainsValue ("20"), "ContainsValue"); DictionaryEntry[] array = new DictionaryEntry[10]; sd.CopyTo (array, 0); sd.Remove ("1"); Assert.AreEqual (9, sd.Count, "Count-9"); sd.Clear (); Assert.AreEqual (0, sd.Count, "Count-0"); }
public void Remove_Null () { StringDictionary sd = new StringDictionary (); sd.Remove (null); }
/// <summary> /// Expands any Unix-style environment variables. /// </summary> /// <param name="commandLine">The command-line to expand.</param> /// <param name="environmentVariables">A list of environment variables available for expansion.</param> private static IList<string> ExpandCommandLine(IEnumerable<ArgBase> commandLine, StringDictionary environmentVariables) { var result = new List<string>(); new PerTypeDispatcher<ArgBase>(ignoreMissing: false) { (Arg arg) => result.Add(FileUtils.ExpandUnixVariables(arg.Value, environmentVariables)), (ForEachArgs forEach) => { string valueToSplit = environmentVariables[forEach.ItemFrom]; if (!string.IsNullOrEmpty(valueToSplit)) { string[] items = valueToSplit.Split( new[] {forEach.Separator ?? Path.PathSeparator.ToString(CultureInfo.InvariantCulture)}, StringSplitOptions.None); foreach (string item in items) { environmentVariables["item"] = item; result.AddRange(forEach.Arguments.Select(arg => FileUtils.ExpandUnixVariables(arg.Value, environmentVariables))); } environmentVariables.Remove("item"); } } }.Dispatch(commandLine); return result; }
/// <summary> /// Sets the public properties of a target object using a string map. /// This method uses .Net reflection to identify public properties of /// the target object matching the keys from the passed map. /// </summary> /// <param name="target">The object whose properties will be set.</param> /// <param name="map">Map of key/value pairs.</param> /// <param name="prefix">Key value prefix. This is prepended to the property name /// before searching for a matching key value.</param> public static void SetProperties(object target, StringDictionary map, string prefix) { Type type = target.GetType(); List<String> matches = new List<String>(); foreach(string key in map.Keys) { if(key.StartsWith(prefix, StringComparison.InvariantCultureIgnoreCase)) { string bareKey = key.Substring(prefix.Length); PropertyInfo prop = type.GetProperty(bareKey, BindingFlags.FlattenHierarchy | BindingFlags.Public | BindingFlags.Instance | BindingFlags.IgnoreCase); if(null != prop) { prop.SetValue(target, Convert.ChangeType(map[key], prop.PropertyType, CultureInfo.InvariantCulture), null); } else { FieldInfo field = type.GetField(bareKey, BindingFlags.FlattenHierarchy | BindingFlags.Public | BindingFlags.Instance | BindingFlags.IgnoreCase); if(null != field) { field.SetValue(target, Convert.ChangeType(map[key], field.FieldType, CultureInfo.InvariantCulture)); } else { throw new NMSException(string.Format("No such property or field: {0} on class: {1}", bareKey, target.GetType().Name)); } } // store for later removal. matches.Add(key); } } // Remove all the properties we set so they are used again later. foreach(string match in matches) { map.Remove(match); } }
public static StringDictionary ExtractProperties(StringDictionary props, string prefix) { if(props == null) { throw new Exception("Properties Object was null"); } StringDictionary result = new StringDictionary(); List<String> matches = new List<String>(); foreach(string key in props.Keys) { if(key.StartsWith(prefix, StringComparison.InvariantCultureIgnoreCase)) { String value = props[key]; result[key] = value; matches.Add(key); } } foreach(string match in matches) { props.Remove(match); } return result; }
// Add to a string dictionary, overridding previous values. private static void Add(StringDictionary dict, String name, String value) { if(dict[name] != null) { dict.Remove(name); } dict[name] = value; }