In this walk through we'll build an interactive Hello World application. Expanding on the simple C# Hello World, we'll make a more advanced version that asks the person using it for their name, and write out a personalized hello to them. We'll also review basic programming concepts along the way.
Review of Basic Terms and Concepts
- Statement: an action; single lines that end in a semicolon, or a series of lines in a code block.
- Variable: a storage location and an associated name (identifier) associated with a value and whose associated value may be changed; a variable can store dynamic (changing) information. The information stored is referenced using the variable name.
- String: a data type that stores a string of characters.
- Operator: an operator is a symbol that produces an action.
- Identifier: names programmers choose for their classes, methods, variables, etc.
- Keywords: names reserved by the compiler.
Create Project
Launch Visual Studio and create a new console Project. New > Project > Console Application. Name it "HelloMe".
You should see something like:
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace HelloMe { class Program { static void Main(string[] args) { } } }
Using Directives
At the top you can see lines that start with the word "using".
The most common reason to have a using directive is to allow the use of types in a namespace without having to specify the fully qualified name - for example:
Console.ReadKey()
instead of
System.Console.ReadKey()
The starting code Visual Studio generated has several using directives. The only line of those we actually need right now is:
using System;
Main
Main is the function that holds the statements we want this application to run. Visual Studio added a parameter to be passed into the Main function (string[] args), which we won't need for this example. In fact all we really need is:
using System; namespace HelloMe { class Program { static void Main() { } } }
Adding Basic Hello World Code
Adding two lines of code will give us a basic Hello World application. The first will write out to the console the string "Hello World" and the second will keep the window open so it doesn't close automatically. (The second line actually reads in from the input stream, and keeps the window open while it is waiting for the input.)
Console.WriteLine("Hello World!"); Console.ReadLine();
We want to add these inside of our Main function code block like this:
static void Main() { Console.WriteLine("Hello World!"); Console.ReadLine(); }
Add those two lines, save (Save All or CTRL-S), and then run your application (use the play button or the F5 key).
Example Code
Your code should look something like this:
using System; namespace HelloMe { class Program { static void Main() { Console.WriteLine("Hello World!"); Console.ReadLine(); } } }
Dot Notation
You can access properties and methods of objects by using dot notation.
Console.WriteLine("Hello World!"); Console.ReadLine();
So in the above code we have two methods that are part of the Console class. We can use these methods by writing Console [dot] MethodName - for example: Console.ReadLine().
[The Console class] Represents the standard input, output, and error streams for console applications. (MSDN)
More About Static
Static
You can see in the definition for the class Console that there are two keywords: public, and static.
public static class Console
We can call the methods WriteLine() and ReadLine() without an instance of the Console class because it is static. Otherwise we would first have to make an instance of the Console class and using the instance name, we would then call the methods.
Common Console Methods
- Console.WriteLine(): Writes the current line terminator to the standard output stream.
- Console.ReadLine(): Reads the next line of characters from the standard input stream.
- Console.ReadKey(): Obtains the next character or function key pressed by the user.
Adding Simple Interaction
We are going to build on the first version to include a simple interaction - we will ask for the name of the person using our application.
Here are our steps:
- Declare a variable that will store the participant's input.
- Write out an instruction for the person to enter their name.
- Read in what the person types.
- Write out "Hello" + the person's name.
We are going to assume that the person actually types in a string of characters. There are many ways to check whether or not the person is entering the type of data we ask for, but for this example we'll keep the coding simple.
Variable Declaration
To use a variable we first need to declare it.
When we declare our variable to store what the person types in for their name, we'll state the data type (String) and a name for the variable (Name).
String Name = "";
Output
Next we want to write out an instruction.
Console.WriteLine("Please enter your name:");
Input
Now we want to add a statement that reads in what is typed by the person using the application, and stores that input in our variable Name.
Name = Console.ReadLine();
Concatenation
Then we want to write out "Hello" and the person's name. We are using the symbol + to concatenate our string and the value of our variable Name.
String values must be inside quote marks. The variable (Name) doesn't need quote marks around it because we want the value of the variable to be printed, not the name of the variable.
Console.WriteLine("Hello " + Name);
And lastly we want to make sure that the window stays open instead of closing immediately after writing out the line that says hello to the person using the application.
Console.ReadLine();
Example Code
Here is our application in its current state:
using System; namespace HelloMe { class Program { static void Main() { String Name = ""; Console.WriteLine("Please enter your name:"); Name = Console.ReadLine(); Console.WriteLine("Hello " + Name); Console.ReadLine(); } } }
Save your file and make sure it runs without errors.
Note: The name is written back out just as it was typed in. If the person who entered their name didn't capitalize the first letter of their name, then it won't be capitalized when printed back out (but there are ways to programmatically capitalize the first letter of a name).
Congratulations! You've created an application that outputs text to the console window, reads in input, and uses saved data to write a custom message to the player!