How to use int.TryParse with nullable int?

I am trying to use TryParse to find out if a string value is an integer. If the value is an integer, skip the foreach loop. Here is my code.

string strValue = "42 " if (int.TryParse(trim(strValue) , intVal)) == false { break; } 

Is intVal a variable of type int? (nullable INT). How can I use tryparse with a nullable int?

+72
c #
Aug 02 '10 at 18:39
source share
5 answers

You cannot do this without using another variable, unfortunately, because the type of the out arguments must exactly match the parameter.

Like the Daniel code, but fixed in terms of the second argument, trimming and avoiding comparisons with boolean constants:

 int tmp; if (!int.TryParse(strValue.Trim(), out tmp)) { break; } intVal = tmp; 
+69
Aug 02 '10 at 18:44
source share

Here's an option for a nullable int with TryParse

 public int? TryParseNullable(string val) { int outValue; return int.TryParse(val, out outValue) ? (int?)outValue : null; } 
+100
Aug 02 '10 at 19:06
source share

Failed to prevent creating a shared version. Use below.

  public class NullableHelper { public delegate bool TryDelegate<T>(string s, out T result); public static bool TryParseNullable<T>(string s, out T? result, TryDelegate<T> tryDelegate) where T : struct { if (s == null) { result = null; return true; } T temp; bool success = tryDelegate(s, out temp); result = temp; return success; } public static T? ParseNullable<T>(string s, TryDelegate<T> tryDelegate) where T : struct { if (s == null) { return null; } T temp; return tryDelegate(s, out temp) ? (T?)temp : null; } } bool? answer = NullableHelper.ParseNullable<bool>(answerAsString, Boolean.TryParse); 
+19
Nov 28 '12 at 11:24
source share

You can create a helper method for analyzing a value with a zero value.

Usage example:

 int? intVal; if( !NullableInt.TryParse( "42", out intVal ) ) { break; } 

Assistant:

 public static class NullableInt { public static bool TryParse( string text, out int? outValue ) { int parsedValue; bool success = int.TryParse( text, out parsedValue ); outValue = success ? (int?)parsedValue : null; return success; } } 
+5
Aug 02 '10 at 18:57
source share

You can also make an extension method for this purpose;

 public static bool TryParse(this object value, out int? parsed) { parsed = null; try { if (value == null) return true; int parsedValue; parsed = int.TryParse(value.ToString(), out parsedValue) ? (int?)parsedValue : null; return true; } catch (Exception) { return false; } } 

I made this extension for type object , but it could also be on string . Personally, I like these parser extenders for any object, therefore extending to object instead of string .

Usage example:

 [TestCase("1", 1)] [TestCase("0", 0)] [TestCase("-1", -1)] [TestCase("2147483647", int.MaxValue)] [TestCase("2147483648", null)] [TestCase("-2147483648", int.MinValue)] [TestCase("-2147483649", null)] [TestCase("1.2", null)] [TestCase("1 1", null)] [TestCase("", null)] [TestCase(null, null)] [TestCase("not an int value", null)] public void Should_parse_input_as_nullable_int(object input, int? expectedResult) { int? parsedValue; bool parsingWasSuccessfull = input.TryParse(out parsedValue); Assert.That(parsingWasSuccessfull); Assert.That(parsedValue, Is.EqualTo(expectedResult)); } 

The disadvantage would be that it violates the syntax of the parsing of values;

 int.TryParse(input, out output)) 

But I like the shorter version (more readable or not, it can be a subject of discussion);

 input.TryParse(out output) 
+3
Jun 28 '13 at 7:28
source share



All Articles