We have seen one way of doing output so far, and that is:
Console.WriteLine("Some text output!");This is the primary way we’ll be handling output for this class.
But… where does Console.WriteLine come from? What is it?
This is a part of the .NET Framework.
The .NET Framework is basically a “starter kit” of sorts, for writing programs. It’s a collection of code you can use across your different programming projects to solve a wide set of problems.
The .NET Framework is automatically added to C# projects when you make them. It is developed and maintained by Microsoft. To use a specific portion of the .NET Framework, you must specifically include it in your code:
using System;The line above tells the compiler that we want to use a particular portion of the starter kit provided to us by .NET, specifically code in the System library. You won’t need to write or use too many additional .NET libraries in this class, but it’s still important to note for future coding projects beyond this class. If you want to test this out, you can remove the using System statement from your code and see what happens!
Output Examples
Output text
(for showing text to the user of the program)
//Write a line of text
Console.WriteLine("Your text here!");Output
Your text here!
Output a text variable
//Write a line of text, with a string variable
string myText = "Your text here!";
Console.WriteLine(myText);Output
Your text here!
Output an integer variable, as text
//write a line of text, with a number attached
int myNumber = 5;
Console.WriteLine("My number is: " + myNumber);Output
My number is 5
Output text and a text variable
//write a line of text, with more text attached
string myFavoriteGame = "Tunic";
Console.WriteLine("My favorite game is " + myFavoriteGame);Output
My favorite game is Tunic
Output text and a variable using formatting
Another way to include variable values in output, instead of using the ’+’ operator, is by adding a ’$’ before the double quotes, then using curly braces and the name of the variable instead! This can be convenient for not having to join strings together all the time.
//write a line of text, with a text variable inserted
string myFavoriteGame = "Silksong";
Console.WriteLine($"ACTUALLY my favorite game is {myFavoriteGame}");Output
ACTUALLY my favorite game is Silksong
Output text without moving to the next line
If you want to write text WITHOUT going to the next line, you can use Console.Write, instead of Console.WriteLine… experiment with using Console.Write multiple times and see what happens!
//write text, no newline
Console.Write("Just writing some text");
Console.Write("And some more!");Output
Just writing some textAnd some more!
This will definitely be useful in certain circumstances!
Input Examples
So we’ve handled output… how do we allow users to interact with our programs? We need a way to enter user input! For that we use the aptly named Console.ReadLine:
When we used Console.WriteLine, it required us to supply it with some text. But Console.ReadLine doesn’t work the same way. This utility is reading user input, and the program can store that user input in a variable! This input comes to the program as text; we can set a new string variable equal to whatever text Console.ReadLine takes into the program:
Input
(for getting user input)
//Read a line of text input from the user
string myText = Console.ReadLine();Result
The value of the string variable
myTextis set to whatever text was entered by the user
OK, this is great and all, we’ve got some text input from the user… but what if we prompt the user for a number? Unfortunately Console.ReadLine doesn’t return us a number, only text. We can, however, try to PARSE that text, and see if it contains a number, like so:
Input, converting text to a number
//Read a line of text input from the user
string aTextNumber = Console.ReadLine();
//Turn text into a number
int inputNumber = int.Parse(aTextNumber);Result
The value of the string variable
aTextNumberis set to whatever text was entered by the user If the user entered a number, the value of the integer variableinputNumberis now that number
So now we can prompt the user of our program to enter some information (by using Console.WriteLine), then actually GET that info from the user (Console.ReadLine). Once our program has done whatever it needs to with the data, we can finally display some results back to the user (Console.WriteLine again).
This back and forth of output/input defines the flow of the console programs we will write in this class. Better get used to Console.WriteLine and Console.ReadLine, cuz they’ll be around the rest of the semester!