Console.ReadLine();
Convert.To____(_____);
commands.While these tutorials have covered a lot of ground, we have not learned anything that would allow us to interact with a user to get information. That means every program we could have created so far will always display exactly the same results, no matter what.
It’s time to fix that.
User input is an important and potentially vast topic. In a video or computer game, you may be getting input from all sorts of hardware, including the keyboard and mouse, gamepads, touchscreens, etc.
In a console application world, we’re only looking at one type of input: text the user types in.
While we’re somewhat limited in what the user can give us, being able to get text and use it within our program will give us a lot more flexibility in what problems we can solve.
The simplest way to get input from a user is with the Console.ReadLine()
method:
string whatTheUserTyped = Console.ReadLine();
This will allow the user to type in text all the way up until they press Enter .
As we saw earlier, the Console
class has several task or methods that it provides and that your program can use.
(We’ll see how to make our own methods in a little while.)
The WriteLine
method is a useful one, as we’ve already seen, but the ReadLine
method is also important.
Some methods have the ability to produce or return
a value.
The Console.WriteLine
method cannot do this, but Console.ReadLine
does.
When the ReadLine
method finishes running, it gives us back a value, which is whatever the user typed.
In the statement above, this value is then stored into the variable whatTheUserTyped
.
With Console.ReadLine
, we can finally make interactive programs:
Console.WriteLine("Say something, and I'll repeat it back to you.");
string text = Console.ReadLine();
Console.WriteLine(text);
While we saw that we can cast between certain types in the previous tutorial, casting doesn’t work across all types. In particular, it doesn’t allow us to convert text to anything else, like a number.
There’s another option: the Convert
class.
The Convert
class has a set of methods that let you give it data of one type and produce philosophically similar data in another type.
For example, the text "3.14"
is technically just text, and the 3
, .
, 1
, and 4
are all characters.
You can’t do math with it in that form.
But if you convert it to its equivalent float
or double
, you can do that math easily.
The Convert
class does these conversions for you.
To illustrate, here’s an example:
Console.WriteLine("What is your favorite number?");
string numberAsText = Console.ReadLine();
int number = Convert.ToInt32(numberAsText);
int nextNumber = number + 1;
Console.WriteLine(number + " is a good number!");
Console.WriteLine("It comes right before " + nextNumber);
All of the methods that Convert
provides start with To
, followed by the type you want to convert to.
For example, ToDouble
will return things of the type double
.
ToByte
will return things of the type byte
.
There are a couple of strange things in the names, however.
Instead of ToInt
and ToLong
, all of the integer types are expressed in the number of bits that they use.
An int
is 4 bytes, and each byte is 8 bits, so ToInt32
will produce things with the type int
, while ToInt64
will produce things with the long
type.
For the unsigned integer types, uint
, ushort
, and ulong
, it is ToUInt32
, ToUInt16
, and ToUInt64
.
The other surprises are that bool
uses the longer term ToBoolean
, while float
is ToSingle
, in contrast to double
, which is ToDouble
.
note
A float
is 4 bytes in size, and is considered “single-precision”, while a double
has 8 bytes, and is considered “double-precision”, which is where the name comes from.
Let’s take a break from introducing new concepts and look at a single, larger program.
The following is a little Cylinder Calculator program, which calculates the volume and surface area of a cylinder, from values that the user types in.
using System;
Console.WriteLine("Welcome to Cylinder Calculator 1.0!");
// Read in the cylinder's radius from the user
Console.Write("Enter the cylinder's radius: ");
string radiusAsAString = Console.ReadLine();
double radius = Convert.ToDouble(radiusAsAString);
// Read in the cylinder's height from the user
Console.Write("Enter the cylinder's height: ");
string heightAsAString = Console.ReadLine();
double height = Convert.ToDouble(heightAsAString);
double pi = 3.1415926536; // We'll learn a better way to do PI in the next tutorial.
// These are two standard formulas for volume and surface area of a cylinder.
// You can find them on Wikipedia: https://en.wikipedia.org/wiki/Cylinder
double volume = pi * radius * radius * height;
double surfaceArea = 2 * pi * radius * (radius + height);
// Now we output the results
Console.WriteLine("The cylinder's volume is " + volume + " cubic units.");
Console.WriteLine("The cylinder's surface area is " + surfaceArea + " square units.");
note
The above program uses top-level statements, so it doesn’t have the namespace
, Program
, and public static void Main
that the template creates.
But you could also simply take that code (all but the first using
directive line at the top) and place it directly in your public static void Main
method, and it would work the same.
This program assembles all of the elements that we have learned so far, and is worth making sure you understand how it works.
We start with a simple display of what the program is. Nothing too surprising.
Then we ask the user for two values: the cylinder’s radius and height.
For each, we store it temporarily in string
-typed variable before converting it to a double
with the Convert.ToDouble
method.
We also define the mathematical number π, though as the comment states, there’s a better way to do that in the future.
We then run our mathematical calculations. The link in the comment takes you to a Wikipedia article that provides the formulas for volume and surface area, which we’ve just turned into code.
Finally, the results are displayed to the user, and the program ends.
challenge
Before you move on, try out this challenge to prove your skills.
Make a program that is a Rectangle Calculator, similar to the Cylinder Calculator above.