Types of TryParse Nullable in General

I wrote TryParse overloaded static methods for the following Nullable types: int? short? long? double? DateTime? , decimal? float? bool? , byte? and char? . The following is part 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 for each method, except that they use different types. It would be impossible to use generics so that I don't have so much redundant code? The signature will look like this:

 bool TryParse<T>(string input, out T value); 

thanks

+6
source share
3 answers

It would be impossible to use generics so that I don't have so much redundant code?

You could do it with reflection, but it would be relatively slow. Otherwise, you could create a map from type to "method to use for this type", but that would be pretty ugly. Among other things, it will never be truly general - it will only work for types that provided the TryParse method for proper signature, which could not be known at compile time.

I would personally think about changing signatures and behavior, by the way. Currently, although the value type is NULL, it will never have a null value at the end of the method, even if you return false . Why not make the return value the result of a parsing operation, returning null on error?

 protected static long? TryParseInt64(string input) { long outValue; return Int64.TryParse(input, out outValue) ? (long?) outValue : null; } 
+9
source

You can use the following general extension method,

 public static Nullable<TSource> TryParse<TSource>(this string input) where TSource : struct { try { var result = Convert.ChangeType(input, typeof(TSource)); if (result != null) { return (TSource)result; } return null; } catch (Exception) { return null; } } 

The next call will return a parsed type with a null value.

 string s = "510"; int? test = s.TryParse<int>(); //TryParse Returns 510 and stored in variable test. string s = "TestInt"; int? test = s.TryParse<int>(); //TryParse Returns null and stored in variable test. 
+5
source

As an aside, you can reorganize your code:

 public static bool TryParse(string input, out int? value) { return TryParse(input, Int32.TryParse, out value); } protected static bool TryParse(string input, out short? value) { return TryParse(input, Int16.TryParse, out value); } protected static bool TryParse(string input, out long? value) { return TryParse(input, Int64.TryParse, out value); } private static bool TryParse<T>(string input, TryParseFunc<T> tryParse, out T? value) where T : struct { T outValue; bool result = tryParse(input, out outValue); value = outValue; return result; } private delegate bool TryParseFunc<T>(string input, out T value); 
+4
source

Source: https://habr.com/ru/post/946240/


All Articles