I have written overloaded static TryParse
methods for the following Nullable
types: int?
, short?
, long?
, double?
, DateTime?
, decimal?
, float?
, bool?
, byte?
and char?
. Below is some of the implementation:
protected static bool TryParse(string input, out int? value)
{
int outValue;
bool result = Int32.TryParse(input, out outValue);
value = outValue;
return result;
}
protected static bool TryParse(string input, out short? value)
{
short outValue;
bool result = Int16.TryParse(input, out outValue);
value = outValue;
return result;
}
protected static bool TryParse(string input, out long? value)
{
long outValue;
bool result = Int64.TryParse(input, out outValue);
value = outValue;
return result;
}
The logic is the same in every method except that they use different types. Would it not be possible to use generics so that I don't need to have so much redundant code? The signature would look like this:
bool TryParse<T>(string input, out T value);
Thanks