Sunday, December 17, 2017

int.TryParse in C#


int.TryParse method is used to convert the given string to its 32-bit signed integer same like int.Parse method but main difference is that it does not throw exception even if the input is not in correct format or is null.

Syntax:

int.TryParse(string str, out convertedValue);
 
str is Input string.
Return Value (convertedValue): A 32-bit signed integer equivalent to “str”.

If the conversion is successful, then given string is converted into its 32-bit signed integer otherwise 0 is returned to the out parameter.


This function returns true for successful conversion and false if conversion is failed. Every time there will be one of two results either 0 if failed or value if successful. There will not be any type of exception thrown by this function even if the input string is:

a)   Null                                      :        NO ArgumentNullException
b)   not in correct format            :        NO FormatException
c)    out of range                         :        NO OverflowException

Share This
Previous Post
Next Post

TekGrabs, established in 2017, is in the business of sharing Information. It provides news, articles and photos about the different technologies, inventions in different domains. Provides information on programming and databases.

0 comments: