In C#, you can display output to the user using the Console.WriteLine()
method. This method takes a string as input and prints it to the console window. Let me give you an example:
Example
using System;
class Program
{
static void Main()
{
// Printing a simple message to the console
Console.WriteLine("Hello, world!");
// You can also output variables along with text
int age = 25;
Console.WriteLine("Your age is: " + age);
// You can use string interpolation for cleaner output
string name = "John";
Console.WriteLine($"Hello, {name}! Welcome to C# programming.");
// You can format output using placeholders and composite formatting
double price = 19.99;
Console.WriteLine("The price is: {0:C}", price); // {0} is replaced by price, and :C formats it as currency
}
}
When you run this program, it will display the following output in the console window:
Hello, world!
Your age is: 25
Hello, John! Welcome to C# programming.
The price is: $19.99
That's it! Console.WriteLine()
is a simple but powerful method for outputting text in C#. You can use it to provide information to the user, display results of calculations, or debug your code.