C# - Output


In C#, there are several ways to output values or print text in console. Here, we will explain Console.WriteLine() and Console.Write() Method. The use of these two method also is the difference between them. There are many other methods for outputting data in C#, such as Console.Error.WriteLine() for writing to the error stream, and System.Diagnostics.Debug.WriteLine() for writing to the debug output window.

Console.WriteLine() Method

The most common method is to use the Console.WriteLine() method, which writes a string of text to the console and adds a newline character at the end.

Example

Console.WriteLine("Rohatash");
Console.WriteLine("Rahul");
Console.WriteLine("Ritesh");
Console.WriteLine("Manoj");

Output

C# Output

You can also use string interpolation to output values along with text. String interpolation allows you to embed expressions inside a string literal by using the $ symbol before the string and enclosing the expressions in curly braces {}.

Example

string name = "Rohatash";
int age = 35;

Console.WriteLine($"My name is {name} and I am {age} years old.");

This will output the text "My name is Rohatash and I am 35 years old." to the console.

Output

C# Output

Console.Write() Method

If you want to output a string without appending a newline character, you can use the Console.Write() method instead.

Console.Write("Rohatash");
Console.Write("Rahul");
Console.Write("Ritesh");
Console.Write("Manoj");

Output

C# Output

This will output the names without a newline character.


Prev Next

Top Articles

  1. Why use C#
  2. How to use comment in C#
  3. How to use variables in C#
  4. How to use keywords in C#