top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

What is the difference between .TryParse and .Parse in C#?

+1 vote
511 views
What is the difference between .TryParse and .Parse in C#?
posted Mar 22, 2017 by Sunil

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button

2 Answers

+1 vote

the main difference between int.Parse(), Convert.ToInt32() and int.TryParse().

int.Parse(string s)

Simply, int.Parse (string s) method converts the string to integer. If string s is null, then it will throw ArgumentNullException. If string s is other than integer value, then it will throw FormatException. If string s represents out of integer ranges, then it will throw OverflowException.

Example

//
// int.Parse(string s)
//

class Program
    {
        static void Main(string[] args)
        {
            string str1="9009";
            string str2=null;
            string str3="9009.9090800";
            string str4="90909809099090909900900909090909"; 
            int finalResult;
            finalResult = int.Parse(str1); //success
            finalResult = int.Parse(str2); // ArgumentNullException
            finalResult = int.Parse(str3); //FormatException
            finalResult = int.Parse(str4); //OverflowException 
        }
    } 

Convert.ToInt32(string s)

Simply, Convert.ToInt32(string s) method converts the string to integer. If string s is null, then it will return 0 rather than throw ArgumentNullException. If string s is other than integer value, then it will throw FormatException. If string s represents out of integer ranges, then it will throw OverflowException.

Example

//
// Convert.ToInt32(string s)
//

class Program
    {
        static void Main(string[] args)
        {
            string str1="9009";
            string str2=null;
            string str3="9009.9090800";
            string str4="90909809099090909900900909090909"; 
            int finalResult;
            finalResult = Convert.ToInt32(str1); // 9009
            finalResult = Convert.ToInt32(str2); // 0
            finalResult = Convert.ToInt32(str3); //FormatException
            finalResult = Convert.ToInt32(str4); //OverflowException 
        }
    }

int.TryParse(string s,Out)

Simply int.TryParse(string s,out int) method converts the string to integer out variable and returns true if successfully parsed, otherwise false. If string s is null, then the out variable has 0 rather than throw ArgumentNullException. If string s is other than integer value, then the out variable will have 0 rather than FormatException. If string s represents out of integer ranges, then the out variable will have 0 rather than throw OverflowException.

Example

//
// Convert.ToInt32(string s)
//

class Program
    {
        static void Main(string[] args)
        {
            string str1="9009";
            string str2=null;
            string str3="9009.9090800";
            string str4="90909809099090909900900909090909"; 
            int finalResult;
            bool output;
            output = int.TryParse(str1,out finalResult); // 9009
            output = int.TryParse(str2,out finalResult); // 0
            output = int.TryParse(str3,out finalResult); // 0
            output = int.TryParse(str4, out finalResult); // 0 
        }
    }

Finally, among all these methods, it is better to use int.TryParse(string s, out int) as this method is the best forever.

answer Mar 22, 2017 by Manikandan J
0 votes

int.Parse() will throw an exception
int.TryParse() will return false (but not throw an ex.

the int.Parse() and int.TryPrase() methods is used to convert a string representation of number to an integer. In case of the string can’t be converted the int.Parse() throws an exceptions where as int.TryParse() return a bool value, false.

Let’s try to understand with a simple example as shown in below.

string val="100":
int value=int.parse(val);

The int.Prase() method throws three different types of exceptions depends on the data provided.

If parameter value is null, then it will throw ArgumentNullException
If parameter value is other than integer value or not in proper format, it will throw FormatException.
if parameter value is out of integer ranges, then it will throw OverflowException.

ncase of int.TryParse() method, when it converts the string representation of an number to an integer; it set the the out variable with the result integer and returns true if successfully parsed, otherwise false.

string val="100";
int result;
bool ifSucess=int.TryParse(100, out result);
answer May 17, 2019 by Siddhi Patel
...