#969 – Creating a Simple Console Application

You’ll most often create applications that have some sort of user interface, e.g. web applications, Windows Store apps, or desktop applications.  You may also create components using C# that have no user interface at all, e.g. class libraries.

For testing purposes, however, it’s helpful to create a simple application that can read from or write to a console window.  The console window appears as a window with a dark background when you run the application.  It can display text one line at a time, or read text entered by the user.  As new text is added to the bottom of the window, earlier text scrolls off the top.

969-002

You create a console application using Visual Studio by selecting the Console Application template when creating a new project.

969-001

You read from or write to the console using methods in the Console class, e.g. ReadLine and WriteLine.

Advertisements

#7 – Reading and Writing from the Console

To read and write from the console, use Console.Read, Console.ReadLine, Console.Write, Console.WriteLine.

namespace ReadWriteConsole
{
    class Program
    {
        static void Main()
        {
            Console.Write("This ");
            Console.WriteLine("is all on one line.");
            Console.Write("Enter your name: ");
            string name = Console.ReadLine();
            Console.WriteLine(name);
            Console.Read();     // pause
        }
    }
}