

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- 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 Questions & Answers
- Difference between void main and int main in C/C++
- Difference between “int main()” and “int main(void)” in C/C++?
- Differentiate between int main and int main(void) function in C
- What is the difference between Firebase and Parse Server?
- What is the difference between const int*, const int * const, and int const *?
- C/C++ difference's between "int main()" and "int main(void)"
- What is the difference between int and integer in MySQL?
- What is the difference between size_t and int in C++?
- What is the difference between int and Int32 in C#?
- What is the difference between Parse Tree and the Syntax Tree?
- What is the main difference between Object.freeze() and const in JavaScript?
- What is difference between int and const int& in C/C++?
- What is the difference between an int and a long in C++?
- Difference Between int and long
- what is the main difference between '=' and '==' operators in javascript?
Advertisements