0Pricing
C# Academy · Lesson

Basic Input and Output

Learn to interact with the user by accepting input and displaying output.

Basic Input and Output is a free C# Academy lesson on CoddyKit — lesson 2 of 3. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the C# Academy learning path, one of 3 lessons in the course, and your progress syncs across the web and the CoddyKit app.

1

Basic Input and Output

Welcome to the next lesson! In this lesson, you’ll learn how to interact with users by taking input and displaying output in a C# program. Let’s get started!

Basic Input and Output  — illustration 1

2

What is Input and Output?

Input refers to data provided by the user, while output is the information displayed by the program. In C#, input is often collected from the console, and output is displayed on it.

Example: A program asks the user for their name (input) and then displays a greeting (output).

3

Displaying Output

To display output in C#, use the Console.WriteLine() method. Example:

Key Point: Use Console.Write() instead of Console.WriteLine() if you don’t want to move to a new line after the output.

using System;

class Program {
    static void Main() {
        Console.WriteLine("Hello, World!"); 
// Outputs: Hello, World!
    }
}

4

Taking Input from the User

To read input from the user, use the Console.ReadLine() method. Example:

Tip: Console.ReadLine() always returns a string, so you may need to convert it for other data types.

using System;

class Program {
    static void Main() {
        Console.WriteLine("What is your name?");
        string name = Console.ReadLine(); 
// Reads input from the user
        Console.WriteLine("Hello, " + name + "!"); 
// Outputs a greeting
    }
}

5

Converting Input

If the user input is a number, convert it using methods like int.Parse() or Convert.ToInt32(). Example:

Key Point: Always validate input to avoid errors when converting types.

using System;

class Program {
    static void Main() {
        Console.WriteLine("Enter your age:");
        int age = int.Parse(Console.ReadLine()); // Converts input to an integer
        Console.WriteLine("You are " + age + " years old.");
    }
}

6

Handling Numeric Input

Use double.Parse() for decimal numbers and handle potential errors using a try-catch block. Example:

Tip: Use try-catch to handle invalid input gracefully.

using System;

class Program {
    static void Main() {
        try {
            Console.WriteLine("Enter a number:");
            double number = double.Parse(Console.ReadLine());
            Console.WriteLine("You entered: " + number);
        } catch (FormatException) {
            Console.WriteLine("Invalid input. Please enter a valid number.");
        }
    }
}

7

Combining Input and Output

Here’s an example program that combines multiple inputs and outputs:

Key Point: Input and output make programs interactive and user-friendly.

using System;

class Program {
    static void Main() {
        Console.WriteLine("What is your name?");
        string name = Console.ReadLine();

        Console.WriteLine("How old are you?");
        int age = int.Parse(Console.ReadLine());

        Console.WriteLine("Hello, " + name + ". You are " + age + " years old.");
    }
}

8

Practice Challenge

Write a program that asks the user for their favorite color and number, then displays a message combining both inputs. Example:

Challenge: Modify the program to handle invalid numeric input using a try-catch block.

using System;

class Program {
    static void Main() {
        Console.WriteLine("What is your favorite color?");
        string color = Console.ReadLine();

        Console.WriteLine("What is your favorite number?");
        int number = int.Parse(Console.ReadLine());

        Console.WriteLine("Your favorite color is " + color + " and your favorite number is " + number + ".");
    }
}

9

10

Great Job!

Congratulations! You’ve learned how to take input and display output in C#, along with handling numeric input. In the next lesson, we’ll discuss comments and code readability to make your programs more organized and maintainable. Let’s keep coding!

Basic Input and Output  — illustration 10

Frequently asked questions

Is the “Basic Input and Output ” lesson free?

Yes — the full text of “Basic Input and Output ” is free to read here on the web, and the C# Academy course includes 3 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the C# Academy course, upgrade to CoddyKit PRO.

What will I learn in “Basic Input and Output ”?

Learn to interact with the user by accepting input and displaying output. You practise C# Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start C# Academy?

No prior experience is required. C# Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 2 of 3, so you can start here or from the beginning and move at your own pace.

How long does the “Basic Input and Output ” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this C# Academy lesson?

Yes. Every C# Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Understanding Variables and Data Types
  2. Basic Input and Output
  3. Understanding Comments and Code Readability
← Back to C# Academy