Professor Mustard Programming
 

Programming 101 - Unit 01 - The Basics

01a) Console Output
 
Let's get right into things and examine a basic C# console application:

using System;

namespace A_ConsoleOutput
{
  class Class1
  {
    [STAThread]
    static void Main(string[] args)
    {
      Console.WriteLine("Hello, world!");
    }
  }
}



All this program does is display "Hello, world!" in a little console window. This might seem like a lot of code to accomplish something so basic, but the good news is that you don't need to worry about most of it right now. Before long, you can understand the full meaning of this entire code segment, but for now, the only part we actually care about is this line:

Console.WriteLine("Hello, world!");


This is the only real instruction in the entire program. We use WriteLine() whenever we want to write text to the console window. Consider the following three lines of code:

Console.WriteLine("One...");
Console.WriteLine("Two...");
Console.WriteLine("Three...");



This would produce the following output on the console:

One...
Two...
Three...



Simple enough. WriteLine() also has a "cousin", called Write():

Console.Write("Four...");
Console.Write("Five...");
Console.Write("Six...");
Console.WriteLine("Now start the next output on a new line!");



This would produce the following output on the console:

Four...Five...Six...Now start the next output on a new line!


As you can see, the difference between WriteLine() and Write() is fairly simple. When you call WriteLine(), the next thing you write to the console will automatically start on a new line. When you use Write(), the next thing you write to the console will append to the current line. And that's pretty much all there is to the basics of console output! Before we move on, though, let's take a quick look at a few other things we can do with it.

If you want a blank line in your output, you can do this:

Console.WriteLine("");


You can also use Environment.NewLine to insert as many blank lines as you want. Use the '+' operator between each "chunk" of the output:

Console.WriteLine("A new line..." + Environment.NewLine + "...has begun!");


The above code would result in the following output:

A new line...
...has begun!



You can insert tabs into your output by putting a "\t" in the text, like so:

Console.WriteLine("Tab\tTab2\tTab3");


The above code would result in the words "Tab Tab2 Tab3", each separated by a tab. What if we want to place quotation marks in the text? Quotation marks have special significance in C#, as they are used to enclose "string literals" (which basically means text, as in "Hello, world!"). If we simply threw some quotation marks into a string, like this...

Console.WriteLine("Ever read "Moby Dick"?");


...C# would become horribly confused, thinking that we meant to end our text just before the word "Moby". We need a way to tell C# that we want to use those quotation marks as plain old punctuation symbols, and not as part of our actual code. To do this, we just throw a backslash in front of each one:

Console.WriteLine("Ever read \"Moby Dick\"?");


Which, by the way, would result in the following output:

Ever read "Moby Dick"?


As we've seen with both tabs and quotes, the backslash character is used to add a special character to your output. In the programming world, we call this an "escape character": the escape character itself is not displayed in the program output, because it only exists to attach extra significance to the character(s) that follow. But, what if we actually want to print out a backslash? Just do this:

Console.WriteLine("This is a backslash: \\");


The first backslash serves its usual role as an "escape character", which allows C# to interpret the second backslash as an ordinary, literal character.

Now that you understand how the backslash works, it's also worth noting that some well-meaning programmers might tell you to create line breaks by inserting a "\n" into your string, instead of using Environment.NewLine. Well, this works in some cases, but not always, and when you get into windows forms it becomes especially picky. If you just stick to using Environment.NewLine, you know that you'll get a proper line break, every time.

One other fun thing you can do with the backslash character is insert special symbols. Click on your Start menu, and navigate to "System Tools/Character Map". Click on any symbol in the grid, and you'll see a special code appear at the bottom of the form (for example, "U+263B"). Back in your program, you can use the "\u" escape sequence, followed by the number you got from the character map, to insert that symbol into your string (in this case, it's a smiley face):

Console.WriteLine("\u263B");


And that's almost everything you need to know to understand the code sample below. There's just one last thing. When the console application has run out of code to execute, it will automatically terminate the program. In other words, it will blast through all the Write() and WriteLine() statements I've added and then immediately close the window before you can even see the output. To prevent this, I've added one extra line of code:

Console.ReadLine();


This forces the whole program to stop until someone hits the "Enter" key. ReadLine() will have greater significance pretty soon, but not quite yet.

Oh, wait. There's one more thing you need to know for now, and it's really important. When you type "//" anywhere in a line of your program, C# will ignore the rest of that line. This is perfect for adding "comments" to your code, like so:

// Greet the world
Console.WriteLine("Hello, world!");



You can put whatever you want after the "//" (which is referred to, technically, as a "comment"). It's great for writing notes within your own code, so make good use of it! Sometimes, professors unintentionally turn their students away from using comments, by forcing them to write a comment for practically every line of code. Generally speaking, comments are best used for explaining your code at a higher level (if you needed a lower-level explanation, you'll probably just read the code anyway). If a specific line of code requires a comment because of its complexity, then obviously do it... but frivolous comments won't help anyone! Don't procrastinate in putting useful comments in your code... you may find it difficult to believe at this stage, but programmers forget how their own code works all the time. Trust me, it will happen, and comments can be lifesavers when it does! Don't neglect them.

Anyways, that's everything you need to understand the first code sample. Basically, only concern yourself with the stuff in the middle of the program. Don't give the { }'s, "static void main", "class", "namespace" and all that other stuff a second thought for now.



using System;

namespace A_ConsoleOutput
{
  class Class1
  {
    [STAThread]
    static void Main(string[] args)
    {
      // *** Actual program starts here ***

      // The '//' indicates that the rest of the line is a comment
      // You can write whatever you want in a comment

      // Write text to the console
      Console.WriteLine("Hello, world!");

      // WriteLine() means that your next output statement starts on a new line
      Console.WriteLine("One...");
      Console.WriteLine("Two...");
      Console.WriteLine("Three...");

      // Write() means that your next output statement starts on the same line
      Console.Write("Four...");
      Console.Write("Five...");
      Console.Write("Six...");
      Console.WriteLine("Now start the next output on a new line!");

      // This simply nserts a blank line
      Console.WriteLine("");

      // Environment.NewLine can insert blank lines in the middle of your message
      // Use the '+' operator between each part of the output
      Console.WriteLine("A new line..." + Environment.NewLine + "...has begun!");

      // '\t' can be used inside your actual string to insert tabs
      Console.WriteLine("Tab\tTab2\tTab3");

      // '\\' inserts backslashes, and '\"' inserts quotes
      Console.WriteLine("Backslash:\\ Quote:\"");

      // Force the program to stay up until the user hits "Enter"
      Console.ReadLine();

      // *** Actual program ends here ***
    }
  }
}