New Batch Starts 1st September 2025 – Join Our .NET Full Stack Development Course, Daily 6-8 AM and 8.30-10.30 PM, Register Now
+91 9989950088
info@example.com
User Input and Output in C#

User Input and Output in C#

Input: Taking data from the user while the program is running.
Output: Displaying results, messages, or information to the user.

In C#, user interaction is done using the Console class from the System namespace. Below is an example:

using System;

class Program
{
  static void Main()
  {
    Console.Write("Enter your name: "); // Input
    string name = Console.ReadLine();

    Console.WriteLine("Hello, " + name + "!"); // Output
  }
}
Input

Read data using Console.ReadLine() or Console.ReadKey().

Output

Display data using Console.Write() or Console.WriteLine().

Why User Input and Output are Important

Dynamic Interaction

Programs become interactive instead of hardcoded, making them more flexible for users.

Data-driven Operations

User input can be used for calculations, validations, and decision-making processes.

Real-world Applications

From command-line tools to large applications, I/O is essential for real-world solutions.

Basic Methods for Input and Output in C#

These methods help in taking input from users and displaying output on the console.

Purpose Method Description
Output Console.Write() Prints data without moving to the next line.
Output Console.WriteLine() Prints data and moves to the next line automatically.
Input Console.Read() Reads the next character entered by the user (ASCII value).
Input Console.ReadLine() Reads the entire line as a string until the Enter key is pressed.
Input Console.ReadKey() Reads a single key press (no Enter needed). Returns ConsoleKeyInfo.
C# User Input & Output Examples
Output Examples in C#
using System;

class Program
{
    static void Main()
    {
        // Using Write()
        Console.Write("Hello ");
        Console.Write("World");  // Output: Hello World (same line)

        // Using WriteLine()
        Console.WriteLine();
        Console.WriteLine("Welcome to C# programming!");
        // Output: Moves to next line automatically
    }
}
        

Explanation:
- Console.Write() prints text without moving to a new line.
- Console.WriteLine() prints text and moves to the next line automatically.

Expected Output:

Hello World
Welcome to C# programming!
Input Examples in C#
using System;

class Program
{
    static void Main()
    {
        // Input using ReadLine()
        Console.Write("Enter your name: ");
        string name = Console.ReadLine();

        Console.WriteLine("Hello, " + name + "!");

        // Input using ReadKey()
        Console.WriteLine("Press any key to continue...");
        Console.ReadKey();
    }
}
        

Explanation:
- Console.ReadLine() reads a full line of text input from the user.
- Console.ReadKey() waits for a single key press and does not require Enter.

Example Interaction / Output:

Enter your name: Megha
Hello, Megha!
Press any key to continue...
        
Converting Input Data Types
using System;

class Program
{
    static void Main()
    {
        Console.Write("Enter your age: ");
        int age = Convert.ToInt32(Console.ReadLine()); // Convert string to int

        Console.WriteLine("Next year, you will be " + (age + 1));
    }
}
        

Explanation:
- Input from Console.ReadLine() is always a string.
- Convert.ToInt32() converts the string input into an integer for numeric operations.

Example Interaction / Output:

Enter your age: 25
Next year, you will be 26
        

Common Conversion Methods: Convert.ToInt32(), Convert.ToDouble(), Convert.ToBoolean(), int.Parse(), double.Parse()

Handling Errors with TryParse
using System;

class Program
{
    static void Main()
    {
        Console.Write("Enter a number: ");
        if (int.TryParse(Console.ReadLine(), out int number))
        {
            Console.WriteLine("You entered: " + number);
        }
        else
        {
            Console.WriteLine("Invalid input! Please enter a number.");
        }
    }
}
        

Explanation:
- int.TryParse() safely converts a string to an integer.
- If the input is invalid, the program does not crash and instead provides an error message.

Example Interaction / Output:

Enter a number: 42
You entered: 42

Enter a number: abc
Invalid input! Please enter a number.
        
5. Advantages & Disadvantages
Advantages
  • Simple and quick for console-based programs.
  • Easy debugging for learning and testing.
  • Works for various data types with conversions.
Disadvantages
  • Limited to console applications only.
  • Not suitable for GUI or web apps (they have other methods).
  • Input always comes as string, requiring conversions.
Product Input & Output in C#

Program: ProductInputDemo.cs


using System;

class Program
{
    static void Main()
    {
        Console.Write("Enter product name: ");
        string product = Console.ReadLine();

        Console.Write("Enter price: ");
        double price = Convert.ToDouble(Console.ReadLine());

        Console.WriteLine($"Product: {product}, Price: {price:C}");
    }
}

        
Enter product name: Laptop
Enter price: 45000
Product: Laptop, Price: ₹45,000.00
        

This program demonstrates user input and output in C#:

  • Console.Write(): Prompts user for input without moving to a new line.
  • Console.ReadLine(): Reads the user's input as a string.
  • Convert.ToDouble(): Converts the string input to a numeric type for calculations.
  • String Interpolation ($"..."): Used to format the output with currency using :C.
  • Finally, the program displays the product name and price formatted as currency.