TheDeveloperBlog.com

Home | Contact Us

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

C# Console.Write Example

This C# example program demonstrates the Console.Write method.

Console.Write puts text on the console window.

Getting started with the Console can be confusing. We learn the difference between Console.Write and Console.WriteLine. We decide which one to choose. We even use them together.

Example. The Console.Write method requires the System namespace to be included at the top to get the fully qualified name. The Write method does not append a newline to the end of the Console.

Using System

Also, if you are printing a string that already has a newline, you can use Console.Write for an entire line. And when you call WriteLine with no parameters, it only writes a newline.

C# program that uses Console.Write

using System;

class Program
{
    static void Main()
    {
	Console.Write("One ");     // <-- This writes the word.
	Console.Write("Two ");     // <-- This is on the same line.
	Console.Write("Three");    // <-- Also on the same line.
	Console.WriteLine();       // <-- This writes a newline.

	Console.WriteLine("Four"); // <-- This is on the next line.
    }
}

Output

One Two Three
Four

The Console.Write method is ideal for when you do not know what you need to output in advance. You could use a StringBuilder for this scenario. But the Write method provides immediate output and requires less code to use.

StringBuilder

Discussion. In most console programs, the actual time required for outputting the text to the screen, mainly in the lower levels of Windows, is far greater than string concatenations. Windows must do many things to display text.

Note: If you concatenate strings before calling Console.Write, porting your code to another program may be more difficult.

string.Concat

WriteLine. We also cover the Console.WriteLine method, which is probably more common in most console-based applications written in the C# language. Knowing how to use Console.Write and Console.WriteLine together was not intuitive to me.

Console.WriteLine

Summary. We used the Console.Write method in the C# language and .NET Framework. We saw the difference between WriteLine and Write. We learned how to use the two together for easily understandable code.

And: Sometimes using format strings is desirable, but they can also make the program less readable.

string.Format


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