Skip to content

Console Input/Output

Input / Output Streams

In C# a stream is an abstract representation of a device on which input and output operations can be performed.

The devices which we can stream to and from are things like the operating system’s standard output (console), standard input (keyboard), and the file system.

You can think of a stream as a source or a destinations for a potentially unlimited sequence of bytes.

The System.Console class is a class that represents the standard input, output and error streams for console applications. The Console class is used for all input and output to the console.

Output Text To the Console

To output text to the console you needed to reference the standard output stream by using Console.Out.

C#
Console.Out

The Out class member returns a reference to a TextWriter object. The TextWriter class has two method for outputting text to a stream:

  • Write() - Prints the specified text to the console without a line break at the end.
  • WriteLine() - Prints the specified text to the console with a line break at the end.
C#
Console.Out.Write("This will output without a line break. ");
Console.Out.WriteLine("This will output with a line break.");

Output:

Text Output
This will output without a line break. This will output with a line break.

These output operations can be simplified by using the Write() and WriteLine() methods of the Console class.

C#
string name = "Chris";

Console.Write("Hello, ");
Console.WriteLine(name + "!");
Console.WriteLine("Today is going to be a great day!");

Output:

Text Output
Hello, Chris!
Today is going to be a great day!

Formatting Output

Composite Formatting

Composite formatting is a feature in C# that allows you to format strings by replacing placeholders with values. The placeholders are enclosed in braces {} and can contain an optional index, alignment, and format string. Here’s an example of composite formatting:

C#
using System;

class Program {
    static void Main(string[] args) {
        string name = "Bing";
        int age = 3;
        Console.WriteLine("Hello, my name is {0} and I am {1} years old.", name, age);
    }
}

The output of this code will be:

Text Output
Hello, my name is Bing and I am 3 years old.

To format a numeric value as currency, you would use the composite format string {index:C}, where index is the index of the argument to substitute into that placeholder.

C#
using System;

class Program {
    static void Main(string[] args) {
        double price = 19.99;
        Console.WriteLine("The price is {0:C}", price);
    }
}

The output of this code will be:

Text Only
The price is $19.99

In the above example, {0:C} is the composite format string. The 0 indicates the index of the argument passed to the Console.WriteLine() method. The C is the format specifier for currency.

You can also use composite formatting to format numbers as percentages. Here’s an example:

C#
using System;

class Program {
    static void Main(string[] args) {
        double percentage = 0.75;
        Console.WriteLine("The percentage is {0:P}", percentage);
    }
}

The output of this code will be:

Text Only
The percentage is 75.00 %

In the above example, {0:P} is the composite format string. The 0 indicates the index of the argument passed to the Console.WriteLine() method. The P is the format specifier for percentage.

Note

Composite formatting also works with the Console.Write() method.

Info

To produce a formatted string, use composite formatting with the String.Format() method.

String Interpolation

You can also use string interpolation to format strings in C#. Here’s an example:

C#
using System;

class Program {
    static void Main(string[] args) {
        string name = "Bing";
        int age = 3;
        Console.WriteLine($"Hello, my name is {name} and I am {age} years old.");
    }
}

The output of this code will be:

Text Only
Hello, my name is Bing and I am 3 years old.

In the above example, the $ symbol is used to indicate that the string is an interpolated string. The curly braces {} are used to enclose the expressions to be evaluated.

Reading From the Console

The Console class can also be used for reading input from the console using the Read() and ReadLine() methods. Read() will read the next character from the stream and ReadLine() reads an entire line of data from the stream.

C#
Console.Write("(Y/N)? ");

char choice = Console.ReadLine()[0];

Console.WriteLine(choice);

Console.Write("Enter your full name: ");

string fullname = Console.ReadLine();

Console.WriteLine(fullname);

Output:

Text Output
(Y/N)? y
y
Enter your full name: Kenny Omega
Kenny Omega

Note

The Console.In class member returns a TextReader object. Using Console.In is not required when you use Console.Read() and Console.ReadLine().

Further Reading