Regex vs Tryparse what is the best in performance

后端 未结 4 1291
日久生厌
日久生厌 2021-01-17 16:25

In my ASP.net project I need to validate some basic data types for user inputs. The data types are like numeric, decimal, datetime etc.

What is the best approach t

4条回答
  •  灰色年华
    2021-01-17 16:31

    I'd guess TryParse is quicker, but more importantly, it's more expressive.

    The regular expressions can get pretty ugly when you consider all the valid values for each data type you're using. For example, with DateTime you have to ensure the month is between 1 and 12, and that the day is within the valid range for that particular month.

提交回复
热议问题