This practice will be retired on May 1, 2025.
Heads up! To view this whole video, sign in with your Courses account or enroll in your free 7-day trial. Sign In Enroll
Well done!
You have completed Practice Basic Variables, Input, and Output in C#!
You have completed Practice Basic Variables, Input, and Output in C#!
Preview
In this video, we'll explain the solution to the challenge.
This video doesn't have any notes.
Related Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign upRelated Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign up
Welcome back.
0:00
How did it go?
0:02
Don't worry if you weren't
able to complete every to do.
0:03
It's not unusual to struggle
when learning something new.
0:06
Let's walk through how I
implemented each to do.
0:09
For the first to do, I declared
a string variable named thing, and
0:15
initialized it to the string
literal value, movie.
0:19
For the second to do, I used the
Console.Write method to output the string
0:27
literal, what is your name,
to the console.
0:31
Then I made a call to the Console.ReadLine
method to prompt the user for their value.
0:35
The ReadLine method returns a string, so
0:40
I capture that value in
a string variable named name.
0:43
Using the Write method here,
instead of the WriteLine method,
0:48
means that the user will input their
value on the same line as the prompt.
0:52
Also, notice this space
after the question mark.
0:57
Adding the space is totally optional, but
1:01
I find that it helps the formatting in
the console to have a little bit of
1:04
breathing room between the prompt text and
the cursor.
1:07
For the third to do,
I use the Console.Write method again
1:18
to output the string literal, what is
your favorite thing, to the console.
1:21
This time, I concatenated the string
literals, what is your favorite, and
1:27
question mark, with the thing variable.
1:31
Then I made a call to the Console.ReadLine
method to prompt the user for their value,
1:36
and capture the method's return value in
a string variable named favoriteThing.
1:41
For the fourth and last to do,
1:51
I used the Console.WriteLine method
to output a message to the console.
1:53
Again, using string concatenation
to combine string literals and
1:57
variable values.
2:02
Using the WriteLine method here
instead of the Write method
2:05
ensures that the message is written
to its own line in the console.
2:09
So when the program terminates,
2:12
the console prompt will be displayed
on the line below the message.
2:14
Be sure to save the file.
2:20
You can do that pressing Ctrl+S or
Cmd+S on the Mac, or
2:22
select the File > Save menu item.
2:26
Now let's compile and run our program.
2:33
Show the console by selecting
the View > Show Console menu item.
2:36
Run the command mcs Program.cs.
2:44
Then we can run the program using
the command mono Program.exe.
2:50
Here's the prompt asking me for
my name and my favorite movie.
2:57
And here is the message,
3:02
my name is James and my favorite movie
is Toy Story, written to the console.
3:07
And that`s it.
3:11
Keep practicing, and
we`ll see you next time.
3:13
You need to sign up for Treehouse in order to download course files.
Sign upYou need to sign up for Treehouse in order to set up Workspace
Sign up