- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
What is the main difference between int.Parse() and Convert.ToInt32 in C#?
Convert a string representation of number to an integer, using the int.Parse or Convert.ToInt32 method in C#. If the string cannot be converted, then the int.Parse or Convert.ToInt32 method returns an exception
Convert.ToInt32 allows null value, it doesn't throw any errors Int.parse does not allow null value, and it throws an ArgumentNullException error.
Example
class Program { static void Main() { int res; string myStr = "5000"; res = int.Parse(myStr); Console.WriteLine("Converting String is a numeric representation: " + res); Console.ReadLine(); } }
Output
Converting String is a numeric representation: 5000
Example
class Program { static void Main() { int res; string myStr = null; res = Convert.ToInt32(myStr); Console.WriteLine("Converting String is a numeric representation: " + res); Console.ReadLine(); } }
Output
Converting String is a numeric representation: 0
Example
class Program { static void Main() { int res; string myStr = null; res = int.Parse(myStr); Console.WriteLine("Converting String is a numeric representation: " + res); Console.ReadLine(); } }
Output
Unhandled exception. System.ArgumentNullException: Value cannot be null.
- Related Articles
- Difference between void main and int main in C/C++
- Difference between “int main()” and “int main(void)” in C/C++?
- What is the main difference between rational and irrational number?
- C/C++ difference's between "int main()" and "int main(void)"
- what is the main difference between '=' and '==' operators in javascript?
- What is the main difference between objects created using object literal and constructor function?
- Convert.ToInt32 Method in C#
- What is the difference Between C and C++?
- C# Convert.ToInt32 Method
- C# int.Parse Method
- State the main difference between asexual and sexual reproduction.
- What is the difference between | and || operators in c#?
- What is the difference between JavaScript and C++?
- What is the main difference between creepers and climbers? Why a creeper can't be a climber?
- What is the difference between "std::endl" and " " in C++?

Advertisements