</>
Skip to content
C# lessons (5/34)

C# — Output

Console.WriteLine

Print text with a newline:

using System;

Console.WriteLine("Hello, World!");
Console.WriteLine("Name: Alice");
Console.WriteLine("Age: 30");

Console.Write

Print without a newline:

using System;

Console.Write("Loading");
for (int i = 0; i < 5; i++)
{
    Console.Write(".");
}
Console.WriteLine(" Done!");

String interpolation

Use $ prefix and {} for variables:

using System;

string name = "Alice";
int age = 30;
double score = 95.5;

Console.WriteLine($"Name: {name}");
Console.WriteLine($"Age: {age}");
Console.WriteLine($"Score: {score:F2}"); // 95.50

// Expressions inside {}
Console.WriteLine($"Next year: {age + 1}");
Console.WriteLine($"Uppercase: {name.ToUpper()}");

Format specifiers

using System;

double price = 1234.56789;
int quantity = 42;

// Currency
Console.WriteLine($"Price: {price:C}");    // $1,234.57

// Fixed-point
Console.WriteLine($"Price: {price:F2}");   // 1234.57

// Number with commas
Console.WriteLine($"Quantity: {quantity:N0}"); // 42
Console.WriteLine($"Quantity: {quantity:N2}"); // 42.00

// Percentage
Console.WriteLine($"Rate: {0.85:P0}");     // 85%
Console.WriteLine($"Rate: {0.85:P2}");     // 85.00%

// Hexadecimal
Console.WriteLine($"Hex: {255:X2}");       // FF

// Scientific
Console.WriteLine($"Sci: {1234567:E2}");   // 1.23E+06

Composite formatting

using System;

string name = "Alice";
int age = 30;

// Index-based
Console.WriteLine("Name: {0}, Age: {1}", name, age);

// Reuse arguments
Console.WriteLine("{0} is {1}. {0} is great.", name, "awesome");

Console colors

using System;

Console.ForegroundColor = ConsoleColor.Green;
Console.WriteLine("This is green text");
Console.ResetColor();

Console.BackgroundColor = ConsoleColor.DarkBlue;
Console.ForegroundColor = ConsoleColor.White;
Console.WriteLine("Blue background, white text");
Console.ResetColor();

Special characters

using System;

Console.WriteLine("Line 1\nLine 2");        // Newline
Console.WriteLine("Tab\there");             // Tab
Console.WriteLine("Quote: \"Hello\"");      // Escaped quote
Console.WriteLine("Backslash: \\");         // Backslash
Console.WriteLine("Dollar: $100");          // Literal $
Console.WriteLine(@"C:\Users\Documents");   // Verbatim string
Console.WriteLine($@"Path: {@"C:\Users"}"); // Interpolated verbatim

Writing to files

using System;
using System.IO;

// Write text
File.WriteAllText("output.txt", "Hello, file!");

// Append text
File.AppendAllText("output.txt", "\nSecond line");

// Read it back
string content = File.ReadAllText("output.txt");
Console.WriteLine(content);

// Write lines
string[] lines = { "Line 1", "Line 2", "Line 3" };
File.WriteAllLines("lines.txt", lines);

Best practices

  • Use $"" string interpolation over concatenation
  • Use format specifiers for consistent number display
  • Use WriteLine for normal output, Write for in-line
  • Use verbatim strings @"..." for file paths

Mini Practice

Write C# code that:

  1. Prints a formatted table of 5 items with prices
  2. Uses format specifiers for currency and percentages
  3. Writes a string interpolation with expressions
  4. Writes output to a file and reads it back

Up Next

In the next lesson, you'll learn about Variables — declaring and using variables in C#.

Related Topics

Frequently Asked Questions about Output

What is Output in C#?

Output is a fundamental concept in C#. This lesson explains it step by step with clear examples, making it easy for beginners to understand.

How do I learn Output?

Start by reading the explanation above, then try the code examples. Practice by modifying the examples and experimenting with different values. Hands-on practice is the best way to learn Output.

Why is Output important in C#?

Output is essential for C# development. Understanding this concept will help you write better code and solve real-world problems more effectively.