Read and Write in C#

ยท

1 min read

The Console class in C# provides methods for interacting with the console, which is the text-based interface used for input and output in console applications. Here are some common use cases and methods associated with the Console class:

1.Writing to the Console

Console.Write() : This method is used to display text on the console without moving to the next line.

Console.WriteLine(): These methods are used to display text on the console and it adds a newline character at the end.

Console.WriteLine("My Name is Manojkumar");

2.Reading from the Console:

Console.ReadLine(): Reads a line of characters from the console.

Console.Write("Enter your name: ");
string name = Console.ReadLine();
Console.WriteLine($"Hello, {name}!");

3.Formatting Output:

String Interpolation (C# 6 and later): Allows you to embed expressions inside string literals.

string name = "John";
int age = 25;
Console.WriteLine($"Name: {name}, Age: {age}");
//Suggested Method to diplay text  
string name = "John";
int age = 25;
Console.WriteLine("Name: {0}, Age: {1}",name,age  );
ย