Home

.NET Framework Classes: Console

   

Introduction to the Console

 

Overview

To allow you to display things on the monitor's screen, the .NET Framework provides a class named Console. The Console class is defined in the System namespace. The System namespace is part of the mscorlib.dll library. When you create a C# application, the mscorlib.dll library is directly available; which means you don't have to import it.

The Console class is static. This means means that you never have to declare a variable from it in order to use it.

Writing to the Console

To provide the ability to display one or more values to the screen, the Console class is equipped with a mehod named Write. To use the Write() method, inside of the parentheses, type the value you want to display. Here is an example:

 
public class Exercise
{
    static void Main()
    {
	System.Console.Write("The Wonderful World of C# Programming");
    }
}

To be able to handle any value of the data types we have used so far, the Write() method is overloaded with various versions, a version that takes an argument for each data type:

public static void Write(char value);
public static void Write(int value);
public static void Write(uint value);
public static void Write(string value);
public static void Write(long value);
public static void Write(ulong value);
public static void Write(float value);
public static void Write(double value);
public static void Write(decimal value);
public static void Write(object value);

Writing With a New Line

After displaying a value on the screen, the Write() method keeps the caret on the same line. To give you the ability to move the caret to the next line after displaying a value, the Console class is equipped with a method named WriteLine. Like Write(), the WriteLine() method has a version for each of the data types we have used so far: 

public static void WriteLine(char value);
public static void WriteLine(int value);
public static void WriteLine(uint value);
public static void WriteLine(string value);
public static void WriteLine(long value);
public static void WriteLine(ulong value);
public static void WriteLine(float value);
public static void WriteLine(double value);
public static void WriteLine(decimal value);
public static void WriteLine(object value);

Besides these versions, the Write() and the WriteLine() methods have each a version that takes an unlimited number of arguments:

public static void WriteLine(. . .);
public static void WriteLine(. . .);

Later on, we will learn how to use these versions to display a value, a series of values, and to specify how the value must be displayed.

To get skeleton code for System.Console.WriteLine, right-click the line where you want to add it and click Insert Snippet... Double-click Visual C#. In the list, double-click cw:

cw

Reading a Value

We saw that the Console class allows using the Write() or the WriteLine() methods to display values on the screen. While the Console.Write() method is used to display something on the screen, the Console class provides the Read() method to get a value from the user. To use it, the name of a variable can be assigned to it. The syntax used is:

VariableName = Console.Read();

This simply means that, when the user types something and presses Enter, what the user had typed would be given (the word is assigned) to the variable specified on the left side of the assignment operator.

Read() doesn't always have to assign its value to a variable. For example, it can be used on its own line, which simply means that the user is expected to type something but the value typed by the user would not be used for any significant purpose. For example some versions of C# would display the DOS window briefly and disappear. You can use the Read() function to wait for the user to press any key in order to close the DOS window.

Reading a Character

As you may know already, each lette of the alphabet is represented on the keyboard by a key. Other symbols, readable (such as #, @, or $) or not (such as Shift, Ctrl, or Enter) are also represented. To get the letter or the action that those keys represent, the Console class is equipped with a method named ReadKey that is overloaded with two versions. One of the versions uses the following syntax:

public static ConsoleKeyInfo ReadKey();

This method takes no argument and produces a value of type ConsoleKeyInfo. To get the value returned by this method, you can declare a ConsoleKeyInfo variable and assign it to the calling of this method.

If the user presses a key for a letter or a readable symbol (such as #, !, /, or %), to recognize the key that was pressed, the ConsoleKeyInfo class is equipped with a member named KeyChar. Here is an example of getting the letter:

using System;

public class Exercise
{
    static int Main()
    {
        ConsoleKeyInfo cki = new ConsoleKeyInfo();

        Console.Write("Press a key: ");
        cki = System.Console.ReadKey();
        Console.WriteLine();

        Console.Write("You pressed: ");
        Console.WriteLine(cki.KeyChar);

        Console.ReadKey();
        return 0;
    }
}

If the key that the user pressed is not a readable symbol such as the Space Bar, a Ctrl key, or Enter, to recognize it, the ConsoleKeyInfo class is equipped with a member named Key. Here is an example of using it:

using System;

public class Exercise
{
    static int Main()
    {
        ConsoleKeyInfo cki = new ConsoleKeyInfo();

        Console.Write("Press a key: ");
        cki = System.Console.ReadKey();
        Console.WriteLine();

        Console.Write("You pressed: ");
        Console.WriteLine(cki.Key);

        Console.ReadKey();
        return 0;
    }
}

When it finishes reading its key, the Console.ReadKey() method stops and lets you decide what to do. If you want it to display the result, the Console class provides another version of the ReadKey() method. Its syntax is:

public static ConsoleKeyInfo ReadKey(bool intercept);

The Boolean argument specifies whether you want the method to display the value.

Reading With a New Line

Besides Read(), the Console class also provides the ReadLine() method. Like the WriteLine() method, after performing its assignment, the ReadLine() method sends the caret to the next line. Otherwise, it plays the same role as the Read() function.

Characteristics and Behaviors of a Console

 

The Title of a Console Screen

A console window is primarily a normal window with classic behaviors. It is equipped with a title bar that displays a title. By default, the title bar displays the path of the Command Prompt:

Title

The Console class allows you to change the title of the console. To do this, assign a string to the Console.Title. Here is an example:

using System;

public class Exercise
{
    public static int Main()
    {
        Console.Title = "Department Store"; 

        return 0;
    }
}

Title

Clearing the Console

If the console screen is filled with text you don't need anymore, you can empty it. To allow you to clear the screen, the Console class is equipped with a method named Clear. It syntax is:

public static void Clear();
 

Previous Copyright © 2010-2016, FunctionX Next