Let's try writing a more complex application. In this lesson, we'll learn how to write a program that gathers input from a user.
We've already practiced working with strings so let's write a program that works with integers. We'll create an application that prompts the user to type in a number, then doubles that number and returns it back to the user.
We'll begin with the following setup:
Create a project directory called double-it
.
In the directory, create a single DoubleIt.csproj
file.
In the DoubleIt.csproj
file, add the following code:
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net5.0</TargetFramework>
</PropertyGroup>
</Project>
This is the exact same boilerplate code we used for our Hello World project's csproj
file.
DoubleIt.cs
file. Add the following basic code to this file:using System;
class DoubleIt
{
static void Main()
{
//program logic will go here
}
}
Note that this file says class DoubleIt
instead of class Program
. We can name our classes whatever we'd like. We'll explore this more in an upcoming lesson.
Let's add code to ask our users a question. We'll do this with two methods:
using System;
class DoubleIt
{
static void Main()
{
Console.WriteLine("Give me a number and I will double it for you!");
int yourNumber = Console.ReadLine();
}
}
Just like before, the Console.WriteLine()
method prints a string to the console.
We also use a method called Console.ReadLine()
, attaching its return value to the variable yourNumber
. This method is used to retrieve input the user has typed into the command line.
Next we'll add code to double this number and print it back to the user:
using System;
class DoubleIt
{
static void Main()
{
Console.WriteLine("Give me a number and I will double it for you!");
int yourNumber = Console.ReadLine();
int yourDoubledNumber = yourNumber * 2;
Console.WriteLine("I doubled your number for you: " + yourDoubledNumber);
}
}
We're simply multiplying the yourNumber
variable by 2
, and printing the results back out to the user with Console.WriteLine()
.
Let's try out our new application. First, we'll compile our file with the command $ dotnet build
.
Unfortunately, we receive an error. It reads:
Build FAILED.
DoubleIt.cs(8,22): error CS0029: Cannot implicitly convert type 'string' to 'int' [/Users/Guest/Desktop/double-it/DoubleIt.csproj]
0 Warning(s)
1 Error(s)
We've seen an error like this before. When we declare a variable's type (like int yourNumber
), we cannot set that variable to a different type of data.
In our case, Console.ReadLine()
actually creates a string
. However, we're trying to save its return value into an int
variable called yourNumber
. C# throws an error to prevent this because it's a strongly-typed language.
Let's fix this error.
using System;
class DoubleIt
{
static void Main()
{
Console.WriteLine("Give me a number, and I will double it for you!");
string stringNumber = Console.ReadLine(); // <-- New code
int yourNumber = int.Parse(stringNumber); // <-- New code
int yourDoubledNumber = yourNumber * 2;
string stringDoubledNumber = yourDoubledNumber.ToString(); // <-- New code
Console.WriteLine("I doubled your number for you: " + stringDoubledNumber);
}
}
Let's walk through our new code:
string stringNumber = Console.ReadLine();
is gathering the user's input in the correct data type, which is string
. We call this variable stringNumber
.
int yourNumber = int.Parse(stringNumber);
defines the yourNumber
variable as the result of transforming stringNumber
from a string
into an int
and uses the built-in int.Parse()
method.
string stringDoubledNumber = yourDoubledNumber.ToString();
takes the result of multiplying our int
by two and turns it back into a string
using the ToString()
method. This way, it can be concatenated with the rest of our statement and printed to the console.
Let's run our program again. If we don't care about seeing build
details, we can compile and run the code with only the dotnet run
command. Let's do that.
$ dotnet run
Now our application works!
.csproj
code:<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net5.0</TargetFramework>
</PropertyGroup>
</Project>
Build a C# program with the following command: dotnet build
.
Run a C# program with the following command: dotnet run
.
The Console.WriteLine()
method prints a string to the console.
The Console.ReadLine()
method is used to retrieve input the user has typed into the command line.
Lesson 3 of 5
Last updated more than 3 months ago.