Tuesday, March 22, 2011

Writing to the Console Window

If you open Visual Studio, then create a new console application, you will discover it is really quite easy to write lines of text to the console window. If you are unsure what a console window looks like, it is very similar to Command Prompt, or you could just look at the image included with this post.
To write lines of text to the console window, you must access the ‘Console’ class object. You do this by typing ‘Console’ and then a period ‘.’ to access its methods and properties. You use the ‘WriteLine()’ method which can accept a string as an argument, such as “Hello user.”

   1:  Module SimpleConsole
   2:   
   3:      Sub Main()
   4:   
   5:          ' Write a line to the console window.
   6:          Console.WriteLine("Hello user.")
   7:   
   8:          ' Wait for the user to press enter before exiting.
   9:          Console.ReadLine()
  10:   
  11:      End Sub
  12:   
  13:  End Module

   1:  using System;
   2:  using System.Collections.Generic;
   3:  using System.Linq;
   4:  using System.Text;
   5:   
   6:  namespace SimpleConsole
   7:  {
   8:      class Program
   9:      {
  10:          static void Main(string[] args)
  11:          {
  12:   
  13:              // Write a line to the console window.
  14:              Console.WriteLine("Hello user.");
  15:   
  16:              // Wait for the user to hit enter before exiting.
  17:              Console.ReadLine();
  18:          }
  19:      }
  20:  }


The above example will produce the following result:
simpleConsole

No comments:

Post a Comment

I Break Code Where code gets done.
ASP.NET | HTML | SQL Server | VB.NET | Request A Topic |