Click here to Skip to main content
14,604,284 members

Difference between int.Parse,Convert.ToInt32 and int.TryParse

Rate this:
4.57 (27 votes)
Please Sign up or sign in to vote.
4.57 (27 votes)
23 Nov 2014CPOL
This tip presents the main difference between int.Parse(), Convert.ToInt32() and int.TryParse().

Introduction

This tip presents 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.

Points of Interest

This is very useful. Finally, I recommend you use int.TryParse(string s, out int).

History

  • 23rd November, 2014: Initial version

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author


Comments and Discussions

 
PraiseGreat article, and here's some benchmarks Pin
Johnny Boyyyy17-Dec-18 21:27
MemberJohnny Boyyyy17-Dec-18 21:27 
QuestionHow about performance? Pin
mtoha22-Feb-16 15:49
professionalmtoha22-Feb-16 15:49 
GeneralUseful Pin
Tipton Tyler25-Nov-14 5:37
MemberTipton Tyler25-Nov-14 5:37 
GeneralRe: Useful Pin
Rapuru Amarendra25-Nov-14 6:38
MemberRapuru Amarendra25-Nov-14 6:38 
Suggestion[My vote of 1] This Misses the Point Entirely Pin
Wastedtalent25-Nov-14 0:17
professionalWastedtalent25-Nov-14 0:17 
QuestionNice Artical!!! Pin
Ram Nunna24-Nov-14 2:50
professionalRam Nunna24-Nov-14 2:50 
AnswerRe: Nice Artical!!! Pin
Rapuru Amarendra24-Nov-14 6:30
MemberRapuru Amarendra24-Nov-14 6:30 
GeneralThoughts PinPopular
PIEBALDconsult23-Nov-14 5:39
professionalPIEBALDconsult23-Nov-14 5:39 
GeneralRe: Thoughts Pin
Andreas Gieriet23-Nov-14 6:17
professionalAndreas Gieriet23-Nov-14 6:17 
QuestionIt's still wrong. PinPopular
OriginalGriff23-Nov-14 4:50
mveOriginalGriff23-Nov-14 4:50 
QuestionGood examples! Pin
Ștefan-Mihai MOGA23-Nov-14 2:55
professionalȘtefan-Mihai MOGA23-Nov-14 2:55 
The right way to do string to int conversion is by using a try-catch blocks, right?
AnswerRe: Good examples! Pin
Rapuru Amarendra23-Nov-14 2:58
MemberRapuru Amarendra23-Nov-14 2:58 
GeneralRe: Good examples! PinPopular
OriginalGriff23-Nov-14 4:54
mveOriginalGriff23-Nov-14 4:54 
GeneralRe: Good examples! Pin
Thanks787223-Nov-14 6:08
professionalThanks787223-Nov-14 6:08 
AnswerRe: Good examples! PinPopular
Aurimas23-Nov-14 4:33
MemberAurimas23-Nov-14 4:33 
GeneralRe: Good examples! Pin
OriginalGriff23-Nov-14 4:54
mveOriginalGriff23-Nov-14 4:54 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Tip/Trick
Posted 23 Nov 2014

Tagged as

Stats

120K views
24 bookmarked