Question: Does C# support bounded generic types in method parameters? If so what is the syntax?
Context: I am writing a utility method that sorts a Dictionary
on the values within the dictionary. Therefore the values in the Dictionary
must implement the IComparable
interface.
Attempt(s): The code below takes a Dictionary
, get the list of KeyValuePair
, sorts on the Value
and then returns the top keys in the dictionary as reported by their values.
private string[] getTopWords<T , U>(Dictionary<T, U> similarWordsAndWeights)
{
var myList = similarWordsAndWeights.ToList();
myList.Sort((firstPair, nextPair) =>
{
return -firstPair.Value.CompareTo(nextPair.Value);
}
);
var dictionary = myList.ToDictionary((keyItem) => keyItem.Key, (valueItem) => valueItem.Value);
return createStringArrayFromGenericList(dictionary.Keys.ToList().GetRange(0, 10));
}
However this code will not compile as U
is not "guaranteed" to implement IComparable
. The syntax private string[] getTopWords<T, U : IComparable>
will also not compile.
I could type check to see that my U
implements IComparable
however I would rather not.
So shortly after going through writing my question I looked on MSDN and found an article on generics which I had been through already however halfway down the page I found the answer that I was looking for. Here is the code for the solution:
private string[] getTopWords<T , U>(Dictionary<T, U> similarWordsAndWeights) where U : IComparable
{
var myList = similarWordsAndWeights.ToList();
myList.Sort((firstPair, nextPair) =>
{
return -firstPair.Value.CompareTo(nextPair.Value);
}
);
var dictionary = myList.ToDictionary((keyItem) => keyItem.Key, (valueItem) => valueItem.Value);
return createStringArrayFromGenericList(dictionary.Keys.ToList().GetRange(0, NUMBER_OF_WORDS_TO_RETURN));
}
It appears that you do the bounding declaration AFTER the parameters. I was looking for something before (having a Java background) and of course was not able to find anything. Amazing what one can do on google with just a few extra minutes...
I thought it wise to share my solution as I was not able to find anything specifically on SO and it may be helpful to others that end up searching for the same thing.