Gigi Labs

Please follow Gigi Labs for the latest articles. Programmer's Ranch no longer has its domain, so please update your bookmarks and links to programmersranch.blogspot.com.

Thursday, May 2, 2013

C# Basics: Input and Output

Hi everyone! :)

In this article we're going to write a small program that can accept input from the user. We're going to do this using the C# language by Microsoft. This is great for super beginners - no experience is required. It's really easy... the article is only long because it contains lots of pictures. :)

Programmers normally use a program called Visual Studio to write C# code. If you're just starting off, there's a free alternative called SharpDevelop which is pretty good, and very lightweight.

So, in SharpDevelop, click on the New Solution button (or File Menu -> New -> Solution...) and create a new console application (under C# -> Windows Applications). Name it as you like, and save it wherever you like. You have just created your first C# project!


So now you are presented with the SharpDeveloper editor, where you can write your C# programs.


 There is some code written for you. The first program you learn to write is usually a program that writes "Hello World", and this sample code is just that. Simply press F5 to run it.


It's pretty simple. You get a console window that displays the text "Hello World!", then asks you to press any key. When you do, the program ends.

If you go back to the sample code, and take a look at the stuff inside the innermost curly brackets...

             Console.WriteLine("Hello World!");
          
            // TODO: Implement Functionality Here
          
            Console.Write("Press any key to continue . . . ");
            Console.ReadKey(true);

...you can begin to guess what the code does. Console.WriteLine() writes the text within brackets and then skips a line. Console.Write() does the same, but doesn't skip a line. Note that the text has to be between quotes ("), so this will cause an error:

             Console.WriteLine(Hello World!);

The Console.ReadKey() thing waits for the user to press a key on the keyboard. Try removing that line, then press F5 to run the program again. What happens? The console window just comes up and disappears, because it does what it has to do (write text) and then terminates - there's nothing to keep it. So put that line back, and let's move on.

The line starting with "//" is a comment - it's just to annotate the code; it doesn't actually do anything. The comment starts at the "//" and runs until the end of the line.

Now, replace the comment with the following lines:

            Console.WriteLine("What's your name?");
            String name = Console.ReadLine();
            Console.WriteLine("Hello, {0}!", name);

OK, so all we're doing here is:

  1. Asking the user to enter his name (just showing text - nothing new here).
  2. Accepting text input via Console.ReadLine(). The user types some text and presses the ENTER key. Whatever he writes gets stored in the name variable.
  3. Using the stored input in the next output. The {0} part refers to the variable we put after the comma, i.e. name.
So once you press F5 and run the program, and enter your name and press ENTER, here's what you get:


Great! This wraps up this very first lesson on C#. I intentionally didn't explain a lot of detail here (e.g. different kinds of comments, data types, etc) because the point was to get you started as quickly as possible. Stay tuned for further tutorials!

No comments:

Post a Comment