-
Notifications
You must be signed in to change notification settings - Fork 0
IDictionary Extensions
Checks if the IDictionary is null or empty.
using UtilityLibrary.Core;
using System;
using System.Collections.Generic;
IDictionary<string, int> dict1 = new Dictionary<string, int>();
IDictionary<string, int> dict2 = null;
bool isNullOrEmpty1 = dict1.IsNullOrEmpty();
bool isNullOrEmpty2 = dict2.IsNullOrEmpty();
// Result: isNullOrEmpty1 = false, isNullOrEmpty2 = trueChecks if the IDictionary is empty, throwing an exception if it is null.
using UtilityLibrary.Core;
using System;
using System.Collections.Generic;
IDictionary<string, int> dict = new Dictionary<string, int>();
bool isEmpty = dict.IsEmpty();
// Result: isEmpty = trueAdds a key-value pair to the IDictionary.
using UtilityLibrary.Core;
using System;
using System.Collections.Generic;
Dictionary<string, int> dict = new Dictionary<string, int>();
dict.Add("Key", 42);Tries to add a key-value pair to the IDictionary. Returns true if added, false if the key already exists.
using UtilityLibrary.Core;
using System;
using System.Collections.Generic;
Dictionary<string, int> dict = new Dictionary<string, int>();
bool success = dict.TryAdd(new KeyValuePair<string, int>("Key", 42));Tries to add a key-value pair to the IDictionary. Returns true if added, false if the key already exists.
using UtilityLibrary.Core;
using System;
using System.Collections.Generic;
Dictionary<string, int> dict = new Dictionary<string, int>();
bool success = dict.TryAdd("Key", 42);Adds a key-value pair to the IDictionary or replaces the value if the key already exists.
using UtilityLibrary.Core;
using System;
using System.Collections.Generic;
Dictionary<string, int> dict = new Dictionary<string, int>();
dict.AddOrReplace("Key", 42);Adds a range of key-value pairs to the IDictionary. Can replace existing values if specified.
using UtilityLibrary.Core;
using System;
using System.Collections.Generic;
Dictionary<string, int> dict = new Dictionary<string, int>();
var newValues = new Dictionary<string, int> { { "Key1", 42 }, { "Key2", 24 } };
dict.AddRange(newValues, replaceExisted: true);Merges multiple dictionaries into one.
using UtilityLibrary.Core;
using System;
using System.Collections.Generic;
Dictionary<string, int> dict = new Dictionary<string, int>();
var dict1 = new Dictionary<string, int> { { "Key1", 42 } };
var dict2 = new Dictionary<string, int> { { "Key2", 24 } };
dict.Merge(dict1, dict2);Gets the value associated with the specified key or a default value if the key is not present.
using UtilityLibrary.Core;
using System;
using System.Collections.Generic;
Dictionary<string, int> dict = new Dictionary<string, int> { { "Key", 42 } };
int value = dict.GetValue("Key"); // Returns 42
int defaultValue = dict.GetValue("NonExistentKey", defaultValue: -1); // Returns -1Gets the value associated with the specified key or adds the key with a default value if not present.
using UtilityLibrary.Core;
using System;
using System.Collections.Generic;
Dictionary<string, int> dict = new Dictionary<string, int>();
int value = dict.GetOrAdd("Key", defaultValue: 42); // Returns 42, and "Key" is added with the default valueRemoves the first occurrence of a specific value from the IDictionary<TKey, TValue>.
using UtilityLibrary.Core;
using System;
using System.Collections.Generic;
Dictionary<string, int> dict = new Dictionary<string, int> { { "Key", 42 } };
bool success = dict.RemoveValue(42);Removes the first occurrence of a specific value from the IDictionary.
using UtilityLibrary.Core;
using System;
using System.Collections;
Hashtable dict = new Hashtable { { "Key", "Value" } };
bool success = dict.RemoveValue("Value");