1

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

2
  • 4
    And the question is? Or you just want to share thoughts... Commented Jan 25, 2012 at 21:43
  • 2
    My suggestion would be to use overloads of the same method instead of using a generic for this. It seems really overkill because in the background you only call the appropriate method. If you have overloads for different types of number you want (double, long, int) it would be really easy to call the appropriate method. Commented Jan 25, 2012 at 21:51

1 Answer 1

1

This is not what generics are made for.

I recommend that you split this method into two methods RandomNumberInt32 and RandomNumberDouble.

There is a way to make this work however:

return (T)(object)RandomNumberLong(min, max); 

But it has nasty performance and is counter-intuitive. I would vastly prefer the specialized methods alternative.

I do not understand why this question was downvoted.

Sign up to request clarification or add additional context in comments.

4 Comments

I considered both the strategies you suggest. In the end even in my example I had overloaded functions internally. But .net has quite a few numeric types and I have similar other examples like this where I am not sure what to do and don't want to do... return (T)(object)RandomNumberLong(min, max); although I will that is quite clever. I wish constraints were more flexible. Actually I think generics were invented to allow us to write say one function that can act on and return many types even though the Collections/Structs/etc. example usages are more common.
My question was what was a best way to do this USING GENERICS!!!! I was aware of the overload approach and am using that and the 'n discrete functions' approach and the clever (T) (Object) approach which I really liked is a "kind of answer" but not complete since it has perf penalties (though small in this case) and could not even in that example keep numbers too large from blowing up the function.
There is no solution using generics. Generics are designed to support infinitely many types. So unfortunately, there is no perfect answer to this question. Is is just impossible. You can use Convert.ToType(typeof(T), ...) btw. But this is just a cast in disguise.
I actually don't mind a cast solution. What I will do is design a function that takes a type in as first parameter and once I check the fact that that type can be cast and is in proper range will cast appropriately. As for generics supporting infinite types constraints actually help narrow what they support and I have used constraints successfully in some situations to support a few types that fit the constraint in a clean high performance way. THANKS EVERYONE FOR CONFIRMING THE OTHER WAYS I AM ALREADY DOING THIS ARE BETTER THAN GENERIC WAY IN THIS CONTEXT. Thannnnnnnnnnnnnnnnnks!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.