C
C#2y ago
Omri_Lahav

❔ C# Beginner

how do i proceed to actually make it? is there a way to store a number in variable to us it later in "if" statement?
10 Replies
Buddy
Buddy2y ago
Equality operators - test if two objects are equal or not equal
C# equality operators test if two objects are equal or not equal. You can define equality operators for your types for custom comparisons for equality
Comparison operators - order items using the greater than and less ...
C# comparison operators check the order of values. The operators >, <, >=, <= compare the order of values. They determine if a value or greater than or less than another value.
Omri_Lahav
Omri_LahavOP2y ago
fml im so dumb xD.
Buddy
Buddy2y ago
$tryparse
MODiX
MODiX2y 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.
Buddy
Buddy2y ago
Convert.ToIn32 / int.Parse will throw an exception if the format is invalid - which may crash your program if unhandled. Always avoid exceptions and try / catch if you can.
Omri_Lahav
Omri_LahavOP2y ago
so how i make it work otherwise?
Buddy
Buddy2y ago
TryParse does not throw an exception, instead it will return a boolean (true / false) whether it succeeded or not. as seen in the example.
Omri_Lahav
Omri_LahavOP2y ago
ho ok
Buddy
Buddy2y ago
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");
}
Accord
Accord2y ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.

Did you find this page helpful?