I use generics a LOT but narrow cases challenge me...
public static T RandomNumberImproved <T>(int min, int max) { bool bolLegit=false; if (typeof(T) == typeof(int)) { bolLegit=true; return (T) RandomNumberLong(min, max); } if (typeof(T) == typeof(double)) { bolLegit=true; return (T) RandomNumberDouble(min, max); } if(!bolLegit) throw new Exception("Unsupported Number Format"); }// end RandomNumberImproved Of course I get errors can't convert to return type T.
Lots of my generic code works great when I can support n types and when constraints help. Cases like this stump me....