Course Content
Introduction to .NET with C#
Introduction to .NET with C#
Taking input from the User
Recap:
-
Console.ReadLine()
statement can be used for taking input from the user, in the form of a string; -
Every built-in data type has a
Parse
method. It can be used for extracting data of that type from a string; -
Giving a value of a very big magnitude (can be positive or negative) for
int.Parse
will crash the program. This happens becauseint
has a limited capacity, and if the number is bigger than that capacity, the program doesn't know what to do, so it crashes. In cases where values of big magnitude are expected, usinglong.Parse
is preferable; -
Using
float.Parse
when dealing with very precise values can potentially cause loss of data becausefloat
has a limited precision, and parsing a very precise value will cause the program to round off the input - therefore causing some loss of precision/data. It is preferable to usedouble.Parse
in such cases; -
A character can be parsed from a string using the
char.Parse()
method. It can also be parsed via indexing as well. So usingConsole.ReadLine()[0]
is a shorter way of writingchar.Parse(Console.ReadLine())
.
Thanks for your feedback!