C# Console.WriteLine

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

Console with lines written by Console.WriteLine

Example

One: 1

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.

Overload

Next:We see the overloads for WriteLine that accept an int, a string and a bool.

IntStringsBool
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
False

It is helpful to know that 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. WriteLine is a static method on the Console type.

Static MethodStatic

Alternatively:You can specify System.Console.WriteLine() to reference the namespace directly.

Empty line

Method call

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.

Environment.NewLine
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

Format strings

Format

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.

string.Concat

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.Format
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, Perls

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.

Same line

Programming tip

You can combine the Console.Write method with the Console.WriteLine method. Both methods can be used on the same line. Write does not append a newline to the end. A detailed example of Console.Write is available.

Console.Write

Char arrays

Array type

This is an advanced feature of Console.WriteLine. It allows you to write an entire char array to the screen. Char arrays are useful in optimization code and sometimes interop or DLL code.

Next:This example first writes the four chars in the array to the screen. It writes the middle two chars.

Char Array Use
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

Colors

Color type

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.

Console.ForegroundColor and Console.BackgroundColor

Summary

Framework: NET

We saw several overloads for the Console.WriteLine method. This method is used in many terminal programs. It lets you display diagnostic messages even in Release mode. Along with Console.Write, it is essential to console applications.

Tip:You can add a Shortcut to your program. This allows you to run your program from the desktop icon.


C#: .NET: Console