C# Console allows users to interact with a C# application via a command-line interface. With the C# Console class, users issue commands to the application in the form of text and the application returns result in the textual form as well. C# Console class comes with various methods and attributes which you can use to interact with the console and customize the console. In this article, you will see how you can work with the C# Console class. You will see how to change the color of the console background and foreground, how to capture user input from the console, and how to print text on the console. You will also see some most of the most commonly used Console class functions. So let’s begin without ado.
Table of Contents
- Changing Console Background & Foreground Colors
- Capturing User Inputs
- Displaying Text on the Console
- Setting Width, Height, and Title
- Miscellaneous Console Functions
Changing Console Background & Foreground Colors
To change the console background and foreground color, you need to set new color values for BackgroundColor and ForegroundColor attributes of the Console class. Let’s first print the default background and foreground color. The Console class is a static class which means that you do not have to make an object of the console class in order to access its methods. You can print values of the BackgroundColor and ForegroundColor attributes of the Console class to print the default background and foreground colors. Look at the following script.
using System; namespace DariaApp { class Program { static void Main(string[] args) { Console.WriteLine("Background color of the console :{0}", Console.BackgroundColor); Console.WriteLine("Foreground color of the console :{0}", Console.ForegroundColor); Console.ReadKey(); } } }
The output below shows that the default background color is black and the foreground color is white.
Output:
Let’s now update the default background and foreground colors. To set the background and foreground color, you can use the ConsoleColor enum which contains values for different colors. You need to assign one of the enum values to the Console.BackgroundColor attribute to set the background color. Similarly, to set the foreground color you need to assign the ConsoleColor enum value to the Console.ForegroundColor attribute.
For example, the following script sets the background console color to green and the foreground console color to red.
using System; namespace DariaApp { class Program { static void Main(string[] args) { Console.BackgroundColor = ConsoleColor.Green; Console.ForegroundColor = ConsoleColor.Red; Console.WriteLine("Background color of the console :{0}", Console.BackgroundColor); Console.WriteLine("Foreground color of the console :{0}", Console.ForegroundColor); Console.ReadKey(); } } }
Output:
From the above output, you can see that the background color of the console is only changed for the area that contains the text. If you wish the change the background color for the whole console, you need to call Clear() method after setting the background color as shown in the following script:
static void Main(string[] args) { Console.BackgroundColor = ConsoleColor.Green; Console.ForegroundColor = ConsoleColor.Red; Console.Clear(); Console.WriteLine("Background color of the console :{0}", Console.BackgroundColor); Console.WriteLine("Foreground color of the console :{0}", Console.ForegroundColor); Console.ReadKey(); }
From the output below, you can see that the background color of the whole console is updated to green.
Output:
Capturing User Inputs
Users interact with a C# Console application via the Console class. Therefore, it is important to capture user input. The Console class in C# provides various methods to capture user input. These methods include Read() and ReadLine().
The Read() method captures a single character input. Look at the following script:
using System; namespace DariaApp { class Program { static void Main(string[] args) { char c = (char)Console.Read(); Console.WriteLine("You typed :" + c) ; Console.ReadKey(); } } }
Output:
Similarly, to read a full line of text instead of a character, you can use the ReadLine() method as shown below:
using System; namespace DariaApp { class Program { static void Main(string[] args) { string line = Console.ReadLine(); Console.WriteLine("You typed :" + line); Console.ReadKey(); } } }
Output:
Displaying Text on the Console
The C# Console class contains different methods that can be used to print text on the console. The most commonly used methods are Write() and WriteLine(). With the Write() method the cursor stays on the same line after a text is printed. On the other hand, with Writeline(), the cursor shifts to a newline after a text is printed on the console.
Let’s first see an example of the Write() method.
using System; namespace DariaApp { class Program { static void Main(string[] args) { Console.Write("This is the first sentence. "); Console.Write("This is the second sentence"); Console.ReadKey(); } } }
From the output, you can see that two sentences printed via the Write() method appear on the same line.
Output:
Let’s now see the WriteLine() method in action.
using System; namespace DariaApp { class Program { static void Main(string[] args) { Console.WriteLine("This is the first sentence. "); Console.WriteLine("This is the second sentence"); Console.ReadKey(); } } }
From the output below, you can see that the WriteLine() method prints every sentence on a new line.
Output:
Setting Width, Height, and Title
You can also modify the width and height of the Console and also assign a custom Console title. To set width and height, you have to specify values for the Console.WindowHeight and Console.WindowWidth attributes. To modify the Console title, you need to set a value for the Console.Title attribute. The following script modifies the height, width, and title of the console window.
using System; namespace DariaApp { class Program { static void Main(string[] args) { Console.WindowHeight = 20; Console.WindowWidth= 40; Console.Title = "My New Title"; Console.Write("This is the first sentence. "); Console.ReadKey(); } } }
Output:
Miscellaneous Console Functions
You can check if the Capslock is on and print True or False accordingly using the Console.CapsLock attribute. To test this functionality, switch on the caps lock on your keyboard and execute the following script.
using System; namespace DariaApp { class Program { static void Main(string[] args) { bool caps = Console.CapsLock; Console.WriteLine(caps); Console.ReadKey(); } } }
You should see the following output:
Output:
By default, the cursor position is displayed on the console. If you want to make the cursor position invisible, you need to set false as the value for the Console.CursorVisible attribute. If you execute the following script, you will see that the cursor will not be visible on the console.
using System; namespace DariaApp { class Program { static void Main(string[] args) { Console.CursorVisible = false; Console.WriteLine("The cursor is not visible"); Console.ReadKey(); } } }
Output:
Finally, you can clear the console using the Clear() function as shown below. If you run the following script, you will be prompted to press the enter key to clear the screen. Once you press enter, all the text will be removed from the console.
using System; namespace DariaApp { class Program { static void Main(string[] args) { Console.WriteLine("Hello press enter to clear the screen"); Console.ReadKey(); Console.Clear(); Console.ReadKey(); } } }
Output:
If you press the enter key now, you will see that the console is cleared as shown below: