Basic calculator application
Hello I just made this application but im not sure if there is a better or more optimized way to make this kind of program. If anyone would be interesting in helping it would be much appreciated!
11 Replies
There is no need to do a convert to string on the Console.Readline
It returns a string right away
what happens if i type "potato" instead of a number?
also, instead of saying "error try again" if they don't choose a valid operator, tell them they chose an invalid operator
Also Convert.To.... isn't the model way of parsing strings to other types
$tryparse
When you don't know if a string is actually a number when handling user input, use
int.TryParse
(or variants, e.g. double.TryParse
)
TryParse
returns a bool
, where true
indicates successful parsing.
Remarks:
- Avoid int.Parse
if you do not know if the value parsed is definitely a number.
- Avoid Convert.ToInt32
entirely, this is an older method and Parse
should be preferred where you know the string can be parsed.
Read more hereUse, e.g., dynamic.TryParse(...)
(i think you mean decimal)
Ah yes
Sorry
Thank you guys for all your feedback Ill make sure to make those changes!
Protip when it comes to
dynamic
: use the banned APIs analyzer to blanket-ban it from every project you startAlso don't forget DRY (Don't Repeat Yourself) principle
Instead of using Console.WriteLine() for each operation
Just output at the end of code