C
C#3y ago
TOLOXI

whats the diffrence between int.Parse and conver.Toint32? [Answered]

whats the diffrence between int.Parse and conver.Toint32?
11 Replies
Pobiega
Pobiega3y ago
int is sane, convert is insane
TOLOXI
TOLOXIOP3y ago
i dont get it does it effect the code?
Pobiega
Pobiega3y ago
yes?
TOLOXI
TOLOXIOP3y ago
how`?
MODiX
MODiX3y ago
Pobiega#2671
REPL Result: Failure
Console.WriteLine(Convert.ToInt32(null));
Console.WriteLine(int.Parse(null));
Console.WriteLine(Convert.ToInt32(null));
Console.WriteLine(int.Parse(null));
Console Output
0
0
Exception: ArgumentNullException
- Value cannot be null. (Parameter 's')
- Value cannot be null. (Parameter 's')
Compile: 611.204ms | Execution: 19.335ms | React with ❌ to remove this embed.
Pobiega
Pobiega3y ago
so with convert, that works and becomes 0 with int.parse, it throws an exception, because null can't correctly be turned into an int Convert has a whole bunch of weird things going on and just straight up should not be used if you are ever not sure the input will 100% always be a correct int, you should only use int.TryParse
TOLOXI
TOLOXIOP3y ago
oh okey thanks for the explination
MODiX
MODiX3y ago
use $tryparse
Unknown User
Unknown User3y ago
Message Not Public
Sign In & Join Server To View
MODiX
MODiX3y ago
The TryParse pattern is considered best practice of parsing data from a string: - a TryParse method returns true or false to inform you if it succeeded or not, so you can use it directly in a condition, - since C# 7 you can declare a variable that will be used as an out argument inline in an argument list, - it forces you to check if the out argument contains valid data afterwards, Avoid: Convert.ToInt32 — it's a bad choice for parsing an int. It exists only for backwards compatibility reasons and should be considered last resort.
return Convert.ToInt32(null); //returns 0. null should not be considered as 0 ever
return Convert.ToInt32("asdf"); //throws FormatException
return Convert.ToInt32(null); //returns 0. null should not be considered as 0 ever
return Convert.ToInt32("asdf"); //throws FormatException
(Note: Convert does contain useful conversion methods: To/FromBase64String, To/FromHexString, ToString(X value, int toBase), ToX(string? value, int fromBase)) Avoid: int.Parse — you have to use a try/catch statement to handle invalid input, which is a less clean solution.
var number = int.Parse("abc"); //throws FormatException
var number = int.Parse(""); //throws FormatException
var number = int.Parse("abc"); //throws FormatException
var number = int.Parse(""); //throws FormatException
Use int.TryParse https://docs.microsoft.com/en-us/dotnet/api/system.int32.tryparse?view=net-5.0#System_Int32_TryParse_System_String_System_Int32__
if (int.TryParse(someInput, out var result))
{
Console.WriteLine($"Thanks for giving me the following number: {result}!");
}
else
{
Console.WriteLine("You didn't give me a valid number :c");
}
if (int.TryParse(someInput, out var result))
{
Console.WriteLine($"Thanks for giving me the following number: {result}!");
}
else
{
Console.WriteLine("You didn't give me a valid number :c");
}
Int32.TryParse Method (System)
Converts the string representation of a number to its 32-bit signed integer equivalent. A return value indicates whether the operation succeeded.
Accord
Accord3y ago
✅ This post has been marked as answered!

Did you find this page helpful?