Skip to content

IDictionary Extensions

Andrzej Kebab edited this page Jan 21, 2024 · 2 revisions

UtilityLibrary.Core

IsNullOrEmpty

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 = true

IsEmpty

Checks 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 = true

Add

Adds 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);

TryAdd

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));

TryAdd (Separate Key and Value)

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);

AddOrReplace

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);

AddRange

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);

Merge

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);

GetValue

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 -1

GetOrAdd

Gets 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 value

RemoveValue

Removes 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);

RemoveValue

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");

Clone this wiki locally