C# Console Output

Console output process in C#.

Console Output

Console output is typically handled using the 'Console' class, which is part of the 'System' namespace. The 'Console' class provides methods to write text to the console, read input from the console, and perform other console-related operations.

Here are some commonly used methods for console output in C#:

'Console.Write' method:

Console.Write("Hi, ");

'Console.Write' is used to print text to the console without a newline character at the end.

'Console.WriteLine' method:

Console.WriteLine(Ayan.");

'Console.WriteLine' is used to print text to the console with a newline character at the end.

Output:
Hi, Ayan.

String formatting:

You can use formatting placeholders to insert values into a string.

string name = "Ayan";
int age = 28;
Console.WriteLine("Name: {0}, Age: {1}", name, age);

You can also use string interpolation, that's allows you to embed expressions directly in string literals, for example:

Console.WriteLine($"Name: {name}, Age: {age}");

Output:
Name: Ayan, Age: 28

Note: These are just a few basic examples of console output in C#. Depending on your needs, you might use other methods provided by the Console class, such as reading input, clearing the console, and more.

Console Colors

You can change the console text and background colors using:
'Console.ForegroundColor'
'Console.BackgroundColor'

cs Copy Code
Console.ForegroundColor = ConsoleColor.Blue; //Text color
Console.BackgroundColor = ConsoleColor.DarkGray; //Background color
Console.WriteLine("The text color & background have been changed.");
Console.ResetColor(); // Reset color
Output:
The text color & background have been changed.

Note: The 'Console.ResetColor()' is used to revert the console color to its default state.

Clear Console

You can clear the console screen by using the 'Console.Clear()' method. Here's a simple example:

cs Copy Code
Console.WriteLine("This is some content on the console.");
Console.Clear();
Console.ReadKey();// Wait for a key press
Output:
 

Note: The 'Console.ReadKey()' method in C# is used to wait for a key press from the user and then return the key that was pressed. This method is often used to pause the program and wait for user input before continuing or exiting.

What is New?

We have just updated this website & still working on adding new content & updating existing features.