Write to Console in C#

Use the Console.WriteLine() method to write text, numbers, Boolean values, characters, variables, and objects to the console in C#. The method converts the supplied value to its text representation, prints it, and then moves the cursor to the next line.

To print content without automatically starting a new line, use Console.Write() instead.

C# Console.WriteLine() Syntax

The following statements show commonly used forms of Console.WriteLine().

</>
Copy
Console.WriteLine();
Console.WriteLine(value);
Console.WriteLine(format, argument0);
Console.WriteLine(format, argument0, argument1);
  • Console.WriteLine() writes only a line terminator, producing an empty line.
  • Console.WriteLine(value) prints the supplied value and then starts a new line.
  • The formatted overloads insert one or more values into placeholders in a format string.

C# Console Output Examples

1. Write a string to the C# console

Following is an example, where we write a string to console output.

C# Program

</>
Copy
using System;

namespace HelloWorld
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Hello World!");
        }
    }
}

Output

PS D:\workspace\csharp\HelloWorld> dotnet run
Hello World!

WriteLine method writes the provided string and ends with a line terminator, which is by default a new line.

2. Write values of different C# data types to the console

C# Program

</>
Copy
using System;

namespace HelloWorld
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Hello World!");
            Console.WriteLine(125);
            Console.WriteLine(true);
            Console.WriteLine(52.14);
            Console.WriteLine(-963);
            string msg = "C# tutorial by TutorialKart";
            Console.WriteLine(msg);
            Console.WriteLine();
            Console.WriteLine('m');
        }
    }
}

Output

PS D:\workspace\csharp\HelloWorld> dotnet run
Hello World!
125
True
52.14
-963
C# tutorial by TutorialKart

m

All the data passed as arguments are first converted to text representations and then printed on to the console.

When you provide no argument to Console.WriteLine(), then it would just print the line terminator (new line). Hence, in the above example, for the last but one WriteLine(), the corresponding output is an empty line followed by new line character.

Also, we can pass an object or a variable to the console.

</>
Copy
string msg = "C# tutorial by TutorialKart";
Console.WriteLine(msg);

In this part of the code, we have passed a variable to the WriteLine.

3. Print multiple values with C# string interpolation

String interpolation places variables directly inside a string. Prefix the string with $ and place each expression inside braces.

</>
Copy
using System;

class Program
{
    static void Main()
    {
        string name = "Arun";
        int score = 92;

        Console.WriteLine($"{name} scored {score} marks.");
    }
}

Output

Arun scored 92 marks.

Interpolation is generally easier to read than joining several strings and variables with the + operator.

4. Format values with Console.WriteLine() placeholders

Console.WriteLine() also supports composite formatting. Placeholders such as {0} and {1} refer to arguments supplied after the format string.

</>
Copy
using System;

class Program
{
    static void Main()
    {
        string product = "Notebook";
        int quantity = 3;

        Console.WriteLine("Product: {0}, Quantity: {1}", product, quantity);
    }
}

Output

Product: Notebook, Quantity: 3

Placeholder indexes start at zero. A placeholder index that does not have a corresponding argument causes a FormatException at runtime.

Console.Write() vs Console.WriteLine() in C#

The difference between these methods is whether a line terminator is written after the value.

MethodBehaviorTypical use
Console.Write()Prints a value without automatically moving to the next line.Prompts and values that should remain on the same line.
Console.WriteLine()Prints a value and then moves to the next line.Messages, results, logs, and line-by-line output.
</>
Copy
Console.Write("Enter your name: ");
string? name = Console.ReadLine();
Console.WriteLine($"Hello, {name}!");

The prompt remains on the same line because it is printed with Console.Write(). After the user enters a value, Console.WriteLine() prints the greeting and ends the line.

How Console.WriteLine() Prints Objects and Null Values

When an object is passed to Console.WriteLine(), C# uses the object’s text representation. For most objects, this means calling the object’s ToString() method.

</>
Copy
using System;

class Student
{
    public string Name { get; set; } = "";

    public override string ToString()
    {
        return $"Student: {Name}";
    }
}

class Program
{
    static void Main()
    {
        Student student = new Student { Name = "Maya" };
        Console.WriteLine(student);
    }
}

Output

Student: Maya

If a null object reference is passed to an overload that accepts an object or string, no text is printed for that value, but Console.WriteLine() still writes the line terminator.

Common C# Console Output Mistakes

  • Using WriteLine when input should stay on the same line: use Console.Write() for prompts such as Enter your name: .
  • Forgetting the $ prefix: without it, braces in an interpolated string are printed as ordinary characters.
  • Using an invalid placeholder index: every composite-format placeholder must have a matching argument.
  • Expecting custom object details automatically: override ToString() when an object needs a meaningful console representation.
  • Confusing program output with terminal commands: commands such as dotnet run are entered in the terminal; the remaining lines are produced by the C# program.

C# Console.WriteLine() Frequently Asked Questions

Does Console.WriteLine() always add a new line?

Yes. After printing the supplied value, Console.WriteLine() writes the platform’s line terminator. Calling it without an argument produces an empty line.

How do I print without a new line in C#?

Use Console.Write(). It prints the supplied value but leaves the cursor on the same line.

Can Console.WriteLine() print multiple variables?

Yes. You can use string interpolation, composite-format placeholders, or string concatenation. String interpolation is often the clearest option for ordinary console messages.

Why does Console.WriteLine() print a class name instead of object data?

If a class does not override ToString(), its inherited implementation may return its type name. Override ToString() to define the text that should represent the object.

C# Console Output Summary

Use Console.WriteLine() to print a value followed by a new line, and use Console.Write() when the next output must remain on the same line. Values can be printed directly, through variables, with string interpolation, or with format placeholders. Objects are displayed using their text representation, which can be customized by overriding ToString().

In this C# Tutorial, we have learned to write string or values belonging to different datatypes to the console.