C# Tutorial
C# String
C# Array
C# Flow Control
C# Class and Object
C# Inheritance
C# Interface
C# Collection
C# Generic
C# File I/O
C# Delegate and Event
C# Exception
C# Process and Thread
C# ADO.NET Database Operations
In this tutorial, we'll cover the basics of console input and output in C# using the Console
class, which is part of the System
namespace. We will explore how to read input from the user and display output to the console.
Create a new C# Console Application project in Visual Studio.
To display output on the console, we can use the Console.WriteLine()
and Console.Write()
methods.
Console.WriteLine()
writes a line of text to the console and appends a newline character. Console.Write()
writes text to the console without appending a newline character.
Console.WriteLine("Hello, World!"); // Writes a line with a newline character Console.Write("Hello, "); // Writes text without a newline character Console.Write("World!");
To read input from the user, we can use the Console.ReadLine()
and Console.ReadKey()
methods.
Console.ReadLine()
reads a line of text from the console and returns it as a string. This method is suitable for reading text input.
Console.ReadKey()
reads a single key press from the console and returns a ConsoleKeyInfo
object. This method is useful for reading single key presses without echoing the input to the console.
// Read a line of text from the user Console.Write("Enter your name: "); string name = Console.ReadLine(); // Read a single key press from the user Console.WriteLine("Press any key to continue..."); ConsoleKeyInfo keyInfo = Console.ReadKey();
In this example, we'll create a simple program that asks the user for their name and age, then displays a personalized message.
static void Main(string[] args) { // Read the user's name Console.Write("Enter your name: "); string name = Console.ReadLine(); // Read the user's age Console.Write("Enter your age: "); int age = Convert.ToInt32(Console.ReadLine()); // Display a personalized message Console.WriteLine($"Hello, {name}! You are {age} years old."); // Wait for the user to press any key Console.WriteLine("Press any key to exit..."); Console.ReadKey(); }
When you run the Console Application, the program will prompt the user for their name and age, then display a personalized message.
In this tutorial, we've covered the basics of console input and output in C# using the Console
class. We demonstrated how to read input from the user and display output to the console, which are essential skills for creating console applications in C#.
Console input in C#:
ReadLine
and ReadKey
.Console.Write("Enter your name: "); string name = Console.ReadLine(); Console.WriteLine($"Hello, {name}!");
Console output in C#:
Write
and WriteLine
methods.Console.WriteLine("This is a line of text."); Console.Write("This is "); Console.Write("on the same line.");
Console formatting options in C#:
string name = "John"; int age = 25; Console.WriteLine("Name: {0}, Age: {1}", name, age);
C# Console colors and styles:
Console.ForegroundColor = ConsoleColor.Green; Console.BackgroundColor = ConsoleColor.Black; Console.WriteLine("Green text on black background"); Console.ResetColor(); // Reset to default colors
Console window manipulation in C#:
Console.WindowHeight = 20; Console.WindowWidth = 50; Console.Clear(); // Clears the console screen