C# Tutorial Part 2.0 – Basic Input

In this part of the tutorial we will make a basic input program which asks you for your name, then says hello to you.

Here goes:

Click to see larger

(Click on the image to see a larger version)
Type that into a new Console application and click debug.
The output should be as follows:

Please enter your name: <you type your name here and press enter>
<Beep!>
Hi there, <whatever you wrote>!

If that didn’t work, check you typed the code correctly and try again.
Now that it works, let’s go through the code:

using System;
Lets the compiler know you will be using things from the system namespace

class BasicInput
{
Starts a class to contain our Main method

static void Main()
{
Starts the Main method, where the program begins

You should already know roughly what these pieces of code all do. If you do not, please read Part 1.0 of the tutorial.

Console.Write(“Please enter your name: “);
Calls the Write method with a string as a parameter. Effectively prints the string (text within the double quotes) to the screen.
You will remember that when we made a Hello World program we used WriteLine. The difference between WriteLine and Write is that WriteLine will start a new line after printing its text, but Write will not. We use write because we want the user to type on the same line we printed the text on

Console.WriteLine(“\aHi there, {0}!”,Console.ReadLine());
This is a little complicated.
In the previous line, there was only one parameter in the parenthesis (known in layman’s terms as brackets). That parameter was a string (text).
This time, there are two parameters: A string and a call to the ReadLine method.
Starting with the most simple part, the \a right at the beginning of the string makes the computer beep. You can try this out in other programs if you like: just put a \a in a string and print it. This has nothing to do with the input, it’s just a cool feature I thought I’d mention.
The next unusual bit is the {0}. This basically means fill the space the {0} takes up with the 0th parameter in the parameter list. The 0th means the first, so the one immediately following the string.
That happens to be a call to the ReadLine method in our example. That method lets the user type something in and waits for them to press enter. The WriteLine method won’t print anything until it’s sure of all its parameters so it lets the ReadLine go first. When the user enters something, WriteLine knows its second parameter, so it fills the gap in the string with the result of the ReadLine, then it prints the string.

Console.ReadLine();
The program would end straight after printing if this line wasn’t here. All it does is delays the end of the program by telling the computer to wait for an input from the user. It won’t do anything with the input, just keep the program on the screen until someone presses enter.

}
}
The closing curly braces finish off the main method and close the class

Try it out

I will make some tasks/questions for you to use to improve your understanding of this lesson if you feel the need to, then the next part of the tutorial will be Part 3.0, Variables.

Advertisements

About Matt

I like writing, filmmaking, programming and gaming, and prefer creating media to consuming it. On the topic of consumption, I'm also a big fan of eating.
This entry was posted in Programming. Bookmark the permalink.

Enter comment:

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s