December 3, 2013

Simple Input and Output

In the last post, we discussed about the important parts of a basic C# program. I hope that from now on you will be comfortable with the language syntax and also with whatever declarations we are supposed to make in a successful program.

This post is dedicated to simple input and output on the console.

So open up your Visual Studio. Some of you may have only C# Express edition, that's fine as well. Open a new project, give it a suitable name and you will find a brand new Program.cs file is on your screen.

One thing that I should have mentioned before, when you open a new project, you will find some default using statements. Your program may or may not use any of the classes under those namespaces. The editor assumes that these are the very basic namespaces that you are going to need, so if you do not use them, no harm done! In my case, I will remove the unnecessary using statements to make sure we understand what we need.

The first thing we would love to see is, taking something as input and display it to the console. You can just copy and paste the codes inside the Main method. 

using System;

namespace Spells
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Hey! What's your name?");
            string input = Console.ReadLine();
            Console.WriteLine("Hello " + input);
            
            Console.ReadLine();  //this will just keep the console visible
        }
    }
}
Hit F5 to build and run. You will find the console, prompting you write your name. Then hit enter. You will probably see this as the final output.
Console Output
What's going on?
The program enters the Main method and finds the statement at line 9. The Console is a class under the System namespace. If you write Console on the editor and then put a period or dot[.] you will notice that the editor is displaying to you some suggestions. These are actually the methods under the Console class. 
Note that once you type "Write"(without the quotes) after that, you will find WriteLine() and Write(). In our case, we have used WriteLine and inside the braces, and inside the double quotes " ", we write something that we want to display on the console. The Write method would gracefully do that too. But the difference is, WriteLine will automatically take you to the next line on the console, while Write will keep you on the same line. Go ahead and do the experiments.

On line 10, we declared a variable of type string. You can name it anything, as long as it does not collide with any other name. Console provides you another method to get user input from the console. The ReadLine method will return an entire line once you hit enter. We do not pass any argument or parameter with ReadLine.  What we do here is, we save the user input inside a string variable.  

On line 11, we use WriteLine again. This time, we make a little modification. This type of modification is called appending two string type "things". The + will append two string objects (we will learn about objects later). Together they will output on the console "Hello" and the name that you supplied just now. But there are other cleaner ways to do that and we will learn that in future.

Do some experiment on your own on these two methods, I am quite sure you will find newer ways to display your desired output. Happy Coding!

No comments:

Post a Comment