-4

I have a function in my code that returns (string, string, string)

return (commonWord.Groups[0].Value, jlpt.Groups[0].Value, wanikaniLevel.Groups[0].Value);

What I would like is for it to return (int?, int?, string)

How can I convert a string to an int?

Alan2
  • 23,493
  • 79
  • 256
  • 450

1 Answers1

1

You can write a method which can convert the string to Nullable<int> if it can convert:

public int? ConvertToInt(string value)
{
   return !string.IsNullOfEmpty(value) && int.TryParse(value, out int intValue) ? return (int?)intValue : null;  
}

and then can change your return tuple statement as

return (ConvertToInt(commonWord.Groups[0].Value), ConvertToInt(jlpt.Groups[0].Value), wanikaniLevel.Groups[0].Value);
user1672994
  • 10,509
  • 1
  • 19
  • 32