Visual C# 2005 Guide
Console Applications

What Is The Console?

The console is an operating system window where users interact with the operating system or a text-based console application by entering text input through the computer keyboard, and reading text output from the computer terminal. For example, in Windows the console is called the command prompt window and accepts MS-DOS commands. The Console class provides basic support for applications that read characters from, and write characters to, the console.

Although Visual C# 2005 has tools for rapid development of form-based applications, it is useful to work with the console first to get a good grounding in the language.

Creating A Console Application

Start by launching C# Express from the Start Menu. Click on File, New Project and choose Console Application in the dialog box. Choose an appropriate name for your first program and click OK.

A window will pop up showing the following code.

console

Any code that we write in our program will go in the space coloured red. We can write as many lines of code as we need at that point but must not delete any of the curly braces or lines of code automatically added for us.

At this stage, you should not worry too much about the meaning of the code that is automatically generated for you. It is important and should not be fiddled with. It will make more and more sense to you as you gain a deeper understanding of programming in C#.

Your First Program

A good first step is to write some code that will output some text to the console window. Write the following two lines of code at the point indicated above.

Console.WriteLine("Hello World!");
Console.ReadLine();

Press F5 to compile and run your first program. You should see the following,

dos window

Press the enter key on the keyboard and the console window will disappear.

There were 2 lines of code. The first line of code outputted the string, Hello World! to the console. It also added a line break (you can see in the screenshot that the cursor is on the second line).

The second line causes the program to wait until the user has pressed the enter key. If you remove this line then the program still works but closes the console window after it has processed the last instruction. We will always need to include a request for keyboard input as our last instruction in a console application.

Notice that we ended our lines of code with a semi-colon. This is a requirement for C#. In the C# language, each instruction must end with a semi-colon. If we have longer instructions, we can write them over several lines (making them easier for us to read) as long as we finish our line of code with the semi-colon. C# is case-sensitive. This means that it matters whether or not you use capital letters in the correct places.

A Second Program

There are two statements that are used for outputting information to the console.

Console.WriteLine()
Console.Write()

We used the first of these in our first program. It writes information to the screen and then adds a line break. The second statement will do the same thing but will not add a line break for you. Try the following instructions,

Console.Write("Hello");
Console.Write(" World!");
Console.ReadLine();

Run your program again. We have a similar effect to our first program but there are subtle differences. Firstly, we have no line breaks in this program. The first Write() statement outputs half of the message to the screen, the second statement does the other half. Try tapping a few keys before you press enter. You will see that the cursor is not on the second line this time.

Console Methods

The basic methods of the console are as follows,

Console.Beep()Makes a beepy noise through the speaker
Console.Beep(frequency, duration)Makes a beepy noise at the frequency specified in hertz (must be a whole number from 37 - 32767) and for the length of time in milliseconds specified by the duration.
Console.Clear()Clears the console window of text
Console.ReadKey()Reads a single key pressed at the console
Console.ReadLine()Reads in a line of input from the console
Console.Write()Outputs information to the screen
Console.WriteLine()Outputs information to the screen and adds a line break

Having A Go

  1. Write a program that outputs a person's name to the console. The program should do nothing until the user presses the enter key. At this point the program should clear the screen, beep and output the text, That's not my name. You can do the whole song if you like!
  2. You may have noticed that the Console.ReadLine() method shows the text that the user types on the screen until they hit the enter key. Write a program that takes advantage of this fact to produce an original story each time the program runs.
  3. Using the WWW or any appropriate reference, write a program that uses the Console.Beep() statement to play a scale.