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

C# Console Class: Console Input And Output

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.

  • Set up the environment

Create a new C# Console Application project in Visual Studio.

  • Console Output

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!");
  • Console Input

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();
  • Console Input and Output Example

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#.

  1. Console input in C#:

    • Description: Demonstrating how to read input from the console using methods like ReadLine and ReadKey.
    • Code:
      Console.Write("Enter your name: ");
      string name = Console.ReadLine();
      Console.WriteLine($"Hello, {name}!");
      
  2. Console output in C#:

    • Description: Illustrating basic console output using the Write and WriteLine methods.
    • Code:
      Console.WriteLine("This is a line of text.");
      Console.Write("This is ");
      Console.Write("on the same line.");
      
  3. Console formatting options in C#:

    • Description: Introducing formatting options in console output using placeholders and string interpolation.
    • Code:
      string name = "John";
      int age = 25;
      Console.WriteLine("Name: {0}, Age: {1}", name, age);
      
  4. C# Console colors and styles:

    • Description: Illustrating how to change console text colors and styles for better visual representation.
    • Code:
      Console.ForegroundColor = ConsoleColor.Green;
      Console.BackgroundColor = ConsoleColor.Black;
      Console.WriteLine("Green text on black background");
      Console.ResetColor(); // Reset to default colors
      
  5. Console window manipulation in C#:

    • Description: Showing how to manipulate the console window, such as changing size and clearing the screen.
    • Code:
      Console.WindowHeight = 20;
      Console.WindowWidth = 50;
      Console.Clear(); // Clears the console screen