本文整理汇总了C#中System.Collections.Specialized.StringDictionary.Remove方法的典型用法代码示例。如果您正苦于以下问题:C# StringDictionary.Remove方法的具体用法?C# StringDictionary.Remove怎么用?C# StringDictionary.Remove使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.Collections.Specialized.StringDictionary
的用法示例。
在下文中一共展示了StringDictionary.Remove方法的14个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Args
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 = "";
}
}
}
}
示例2: MergeTo
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;
}
}
示例3: Main
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);
}
示例4: Empty
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 ();
}
示例5: InstallContext
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);
}
示例6: Test01
//.........这里部分代码省略.........
//
// 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 "));
}
}
示例7: Test01
//.........这里部分代码省略.........
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(); });
}
示例8: Test01
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++)
//.........这里部分代码省略.........
示例9: SomeElements
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");
}
示例10: Remove_Null
public void Remove_Null ()
{
StringDictionary sd = new StringDictionary ();
sd.Remove (null);
}
示例11: ExpandCommandLine
/// <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;
}
示例12: SetProperties
/// <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);
}
}
示例13: ExtractProperties
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;
}
示例14: Add
// 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;
}