C# Display Variables - Complete Guide
Introduction to Displaying Variables
Displaying variable values is a fundamental task in C# programming. The Console class in the System namespace provides methods such as Write and WriteLine to send output to the console.
C# supports different ways to format output, including string concatenation, composite formatting, and string interpolation. Each technique is useful in different scenarios depending on clarity and formatting needs.
Methods for Displaying Variables
The most common ways to display variables in C# are string concatenation, composite formatting, and string interpolation:
Example
using System;
namespace DisplayVariables
{
class Program
{
static void Main(string[] args)
{
string name = "Alice";
int age = 25;
double salary = 55000.50;
DateTime hireDate = new DateTime(2020, 6, 15);
// 1. String concatenation
Console.WriteLine("Name: " + name + ", Age: " + age);
// 2. Composite formatting with placeholders
Console.WriteLine("Name: {0}, Age: {1}, Salary: {2:C}", name, age, salary);
// 3. String interpolation (C# 6.0+)
Console.WriteLine($"Name: {name}, Age: {age}, Salary: {salary:C}");
// 4. Multiple Write/WriteLine calls
Console.Write("Employee: ");
Console.Write(name);
Console.Write(" was hired on ");
Console.WriteLine(hireDate.ToString("MMMM dd, yyyy"));
// 5. Using StringBuilder for more complex assembly
var sb = new System.Text.StringBuilder();
sb.Append("Details: ");
sb.Append(name);
sb.Append(" (");
sb.Append(age);
sb.Append(" years old) earns ");
sb.Append(salary.ToString("C"));
Console.WriteLine(sb.ToString());
}
}
}
Output
Name: Alice, Age: 25 Name: Alice, Age: 25, Salary: $55,000.50 Name: Alice, Age: 25, Salary: $55,000.50 Employee: Alice was hired on June 15, 2020 Details: Alice (25 years old) earns $55,000.50
Formatting Variable Output
C# provides formatting features for numbers, currency, dates, and alignment. These make console output more user-friendly and professional.
Example
using System;
namespace FormattingOutput
{
class Program
{
static void Main(string[] args)
{
int number = 1234567;
double price = 1234.5678;
DateTime today = DateTime.Now;
decimal amount = 12345.67m;
// Number and currency formatting
Console.WriteLine("Number: {0:N}", number); // 1,234,567.00
Console.WriteLine("Number: {0:N0}", number); // 1,234,567
Console.WriteLine("Price: {0:C}", price); // $1,234.57
Console.WriteLine("Amount: {0:C}", amount); // $12,345.67
// Date formatting
Console.WriteLine("Short date: {0:d}", today);
Console.WriteLine("Long date: {0:D}", today);
Console.WriteLine("Time: {0:t}", today);
Console.WriteLine("Full: {0:F}", today);
Console.WriteLine("Custom: {0:yyyy-MM-dd}", today);
// Alignment and padding
string[] products = { "Laptop", "Mouse", "Keyboard" };
decimal[] prices = { 999.99m, 25.50m, 75.25m };
Console.WriteLine("\n{0,-15} {1,10}", "Product", "Price");
Console.WriteLine(new string('-', 26));
for (int i = 0; i < products.Length; i++)
{
Console.WriteLine("{0,-15} {1,10:C}", products[i], prices[i]);
}
}
}
}
Output
Number: 1,234,567.00 Number: 1,234,567 Price: $1,234.57 Amount: $12,345.67 Short date: 10/15/2023 Long date: Sunday, October 15, 2023 Time: 2:30 PM Full: Sunday, October 15, 2023 2:30:45 PM Custom: 2023-10-15 Product Price -------------------------- Laptop $999.99 Mouse $25.50 Keyboard $75.25