changeset 28:41ddcd61dc92

Add method to get array of keys from dictionary no-open-ticket
author IBBoard <dev@ibboard.co.uk>
date Thu, 09 Apr 2009 15:10:51 +0000
parents 4ea1fc351533
children e38192f55d2d
files Collections/DictionaryUtils.cs
diffstat 1 files changed, 27 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- a/Collections/DictionaryUtils.cs	Sat Mar 28 20:54:01 2009 +0000
+++ b/Collections/DictionaryUtils.cs	Thu Apr 09 15:10:51 2009 +0000
@@ -35,6 +35,33 @@
 			
 			return col;
 		}
+		
+		/// <summary>
+		/// Takes the set of keys in a dictionary and returns them as an array of typed objects.
+		/// </summary>
+		/// <param name="dictionary">
+		/// A <see cref="IDictionary"/> to extract an array of keys from
+		/// </param>
+		/// <returns>
+		/// An array of <see cref="KEY_TYPE"/> objects taken from the Keys property of the dictionary, or NULL if the dictionary is NULL
+		/// </returns>
+		public static  KEY_TYPE[] ToKeyArray<KEY_TYPE, VALUE_TYPE>(IDictionary<KEY_TYPE, VALUE_TYPE> dictionary)
+		{
+			if (dictionary == null)
+			{
+				return null;
+			}
+			
+			int entryCount = dictionary.Count;
+			KEY_TYPE[] col = new KEY_TYPE[entryCount];
+			
+			if (entryCount > 0)
+			{
+				dictionary.Keys.CopyTo(col, 0);
+			}
+			
+			return col;
+		}
 
 		/// <summary>
 		/// Takes two dictionaries and merges them. If a key exists in both dictionaries then the value in <code>firstDictionary</code> takes priority.