次の型のオーバーロードされた静的TryParse
メソッドを作成しました: 、、、、、、および。以下は実装の一部です。Nullable
int?
short?
long?
double?
DateTime?
decimal?
float?
bool?
byte?
char?
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;
}
ロジックは、異なる型を使用することを除いて、すべてのメソッドで同じです。冗長なコードをあまり必要としないように、ジェネリックを使用することはできないでしょうか? 署名は次のようになります。
bool TryParse<T>(string input, out T value);
ありがとう