In C#, output is commonly displayed to the console using the Console.WriteLine()
and Console.Write()
methods.
This method prints the text followed by a newline (moves to the next line):
Console.WriteLine("Hello, World!");
Console.WriteLine("Welcome to C# programming.");
This prints the text without adding a new line after it, so the next output continues on the same line:
Console.Write("Enter your name: ");
Console.Write("John");
You can output variable values by concatenating them with strings using the +
operator:
string name = "Alice";
int age = 25;
Console.WriteLine("Name: " + name);
Console.WriteLine("Age: " + age);
String interpolation makes it easier to embed variables inside strings:
Console.WriteLine($"Name: {name}");
Console.WriteLine($"Age: {age}");
You can format numbers, dates, and more using format strings:
double price = 123.456;
Console.WriteLine($"Price: {price:F2}"); // Outputs price with 2 decimal places
Summary
Console.WriteLine()
prints text and moves to a new line.Console.Write()
prints text without moving to a new line.Help others discover Technorank Learning by sharing your honest experience.
Your support inspires us to keep building!