Console.WriteLine renders a line of text on the console. It has many overloads. It is one of the most useful Console methods. In its simplest form it outputs the string argument with a trailing newline. With no arguments it outputs a blank line.

These C# example programs show the Console.WriteLine method. Console.WriteLine has many overloads.
First, there are many overloads on Console.WriteLine. An overloaded method has the same name but accepts different parameters. The overload can have different types of parameters, different numbers of parameters, or both. We see the overloads for WriteLine that accept an int, a string and a bool.
Program that uses Console.WriteLine [C#]
using System;
class Program
{
static void Main()
{
// Write an int with Console.WriteLine.
int valueInt = 4;
Console.WriteLine(valueInt);
// Write a string with the method.
string valueString = "Your string";
Console.WriteLine(valueString);
// Write a bool with the method.
bool valueBool = false;
Console.WriteLine(valueBool);
}
}
Output
4
Your string
FalseNotes. When you use the Console class, you do not need to create a new Console(). You need to add the "using System" line at the start. Alternatively you can specify "System.Console.WriteLine()" to reference the namespace directly.
Next, it is possible to use Console.WriteLine with no arguments. This will simply output a blank line to the Console window. This is an elegant way to output an empty line. You do not have to specify the newline character.
Program that uses no arguments [C#]
using System;
class Program
{
static void Main()
{
Console.WriteLine("A");
Console.WriteLine(); // Empty line
Console.WriteLine("B");
}
}
Output
A
B
In some programs, you will want to write several values on a single line to the Console. One way you can do this is by appending the string using the + operator before passing it to Console.WriteLine. It is often clearer to use a format string. When using a format string, the first parameter is a literal that has N substitution places. The next N parameters are inserted in those places.
string.Concat [+ operator]Program that uses Console.WriteLine with format strings [C#]
using System;
class Program
{
static void Main()
{
string value1 = "Dot";
string value2 = "Net";
string value3 = "Perls";
Console.WriteLine("{0}, {1}, {2}", // <-- This is called a format string.
value1, // <-- These are substitutions.
value2,
value3);
}
}
Output
Dot, Net, PerlsOutput. The example C# code will print the words "Dot, Net, Perls" to the Console. The commas in the format string are retained and printed unchanged. Please remember to start counting at 0 for the format string substitution markers.
string.Format
You can combine the Console.Write method with the Console.WriteLine method. The Write method does not append a newline to the end of the Console. A detailed example of using the Write method along with WriteLine is also available at this site.
Console.Write
This is one of the more advanced features of Console.WriteLine. It allows you to write an entire char array to the screen. Char arrays are useful in optimizations and sometimes interop code. This example first writes the four chars in the array to the screen. Next it writes the middle two chars.
char[]Program that uses Console.WriteLine with arrays [C#]
using System;
class Program
{
static void Main()
{
char[] array = new char[] { 'a', 'b', 'c', 'd' };
//
// Write the entire char array on a new line.
//
Console.WriteLine(array);
//
// Write the middle 2 characters on a new line.
//
Console.WriteLine(array, 1, 2);
}
}
Output
abcd
bc
You can combine Console.Write and WriteLine with Console coloring. This can really enhance the usability of your console programs. Other methods, such as Clear, can really make your life easier. In my experience, Console.WriteLine is used much more than Write.
Console.ForegroundColor and Console.BackgroundColor
We saw several overloads for the Console.WriteLine method. This method is used in almost every terminal program. It lets you see diagnostic messages even in Release mode. I suggest you add a Shortcut to your program. This allows you to run your program from the desktop icon.
Console Programs