You cannot return either a double
or an int
from a generic method without it also returning any other type.
I might, for example, have a Foo
class and your generic parse method, without any constraint, will allow this call to be made:
Foo result = Parse<Foo>("111");
The best that you can do with numbers is constrain on your function by only allowing struct
(value-types) to be used.
T Parse<T>(string value) where T : struct;
But this will allow all number types, plus any other value-type.
You can constrain by interface type, but there isn't an INumeric
interface on double
or int
so you're kind of stuck.
The only thing that you can do is throw an exception if the wrong type is passed in - which generally isn't very satisfying.
Your best approach, in this case, is to abandon generics and use separately named methods.
double ParseDouble(string value);
int ParseInteger(string value);
But, of course, this won't help you learn generics. Sorry.