Skip to content

Latest commit

 

History

History
47 lines (35 loc) · 1.03 KB

GCop124.md

File metadata and controls

47 lines (35 loc) · 1.03 KB

GCop 124

"Use numeric string.TryParseAs< data type >() instead of data type.TryParse(numeric string)"

Rule description

The TryParseAs<...>() extension method on the string type allows you to make safe type conversions in a uniform way for many types. If the value cannot be converted to the target type then it returns null instead of throwing an exception. It's briefer and more readable than alternative styles of code for handling this.

Example 1

decimal myDecimal;

if (decimal.TryParse(txtUserInput.Text, out myDecimal))
{
   someObject.SomeNullableProperty = myDecimal;
}
else
{
   someObject.SomeNullableProperty = null;
}

should be 🡻

someObject.SomeNullableProperty = txtUserInput.Text.TryParseAs<decimal>();

Example 2

try
{
    someObject.SomeNullableProperty = decimal.Parse(txtUserInput.Text);
}
catch
{
    someObject.SomeNullableProperty = null;
}

should be 🡻

someObject.SomeNullableProperty = txtUserInput.Text.TryParseAs<decimal>();