int.TryParse syntatic sugar

后端 未结 10 1393
广开言路
广开言路 2020-12-14 14:15

int.TryPrase is great and all, but there is only one problem...it takes at least two lines of code to use:

int intValue;
string stringValue = \"         


        
10条回答
  •  春和景丽
    2020-12-14 14:44

    Check out the StringExtensions class. It contains an AsInt(String,Int32) extension method that will attempt to convert a string and if unsuccessful populate it with the supplied Int32 value as default.

    Example:

    var intValue = "123".AsInt(-1);
    

提交回复
热议问题