TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

C# Console.WriteLine

These C# example programs show the Console.WriteLine method. Console.WriteLine has many overloads.

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.

Example. 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.

IntBool

C# program that uses Console.WriteLine

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

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

Empty line. 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

C# program that uses no arguments

using System;

class Program
{
    static void Main()
    {
	Console.WriteLine("A");
	Console.WriteLine(); // Empty line.
	Console.WriteLine("B");
    }
}

Output

A

B

Format strings. 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

C# program that uses Console.WriteLine with format strings

using System;

class Program
{
    static void Main()
    {
	string value1 = "Dot";
	string value2 = "Net";
	string value3 = "Perls";

	Console.WriteLine("{0}, {1}, {2}",
	    value1,
	    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. 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. 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

C# program that uses Console.WriteLine with arrays

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


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf