Skip to content

Instantly share code, notes, and snippets.

@DorukUlucay
Last active April 20, 2020 07:01
Show Gist options
  • Save DorukUlucay/42fea77157babc95c0208f8ac06666c4 to your computer and use it in GitHub Desktop.
Save DorukUlucay/42fea77157babc95c0208f8ac06666c4 to your computer and use it in GitHub Desktop.
tips & tricks

Parsing Strings into Numbers with the NumberStyles Enumeration

// throws System.FormatException
var number = int.Parse(" 1,000 ");

// parses into 1000
var number = int.Parse(" 1,000 ", System.Globalization.NumberStyles.AllowThousands | System.Globalization.NumberStyles.AllowLeadingWhite | System.Globalization.NumberStyles.AllowTrailingWhite);

source: https://codewithshadman.com/csharp-number-and-datetime-tips

Preventing Ambiguous DateTime Parsing and Mis-parsing

//parses
var d1 = DateTime.ParseExact("02/21/2020", "MM/dd/yyyy", null);

//throws exec
var d2 = DateTime.ParseExact("21/02/2020", "MM/dd/yyyy", null);

source: https://codewithshadman.com/csharp-number-and-datetime-tips

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment