Skip to content

Instantly share code, notes, and snippets.

@jarrettmeyer
Last active August 29, 2015 14:07
Show Gist options
  • Save jarrettmeyer/e69fdfb55f9e241f1a42 to your computer and use it in GitHub Desktop.
Save jarrettmeyer/e69fdfb55f9e241f1a42 to your computer and use it in GitHub Desktop.
public class TryParseInt32Result
{
private readonly bool _isSuccessful;
private readonly int _value;
public TryParseInt32Result(bool isSuccessful, int value)
{
_isSuccessful = isSuccessful;
_value = value;
}
public bool IsSuccessful
{
get { return _isSuccessful; }
}
public int Value
{
get { return _value; }
}
}
public static TryParseInt32Result TryParse(string str)
{
int value;
bool isSuccessful = TryParse(str, out value);
return new TryParseInt32Result(isSuccessful, value);
}
// Usage:
var result = System.Int32.TryParse("123");
if (result.IsSuccessful)
{
var theValue = result.Value;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment