Extension methods to Dictionaries

FindKey<T>(T)

Finds the key for a specified value.

IDictionary<int, string> numbers = new Dictionary<int, string>()
{
	{ 1, "one" },
	{ 2, "two" },
	{ 3, "three" }
};

int number = numbers.FindKey("two"); // 2

Swap()

Swaps keys and values in a given dictionary.

IDictionary<int, string> numbers = new Dictionary<int, string>()
{
	{ 1, "one" },
	{ 2, "two" },
	{ 3, "three" }
};
IDictionary<string, int> reverseLookup = numbers.Swap();

int number = reverseLookup["two"];

Last edited Nov 13, 2012 at 4:34 PM by BlueCode, version 1

Comments

No comments yet.