Skip to content

CSharp Basics FirstConsoleApp Input

Joe Care edited this page Dec 22, 2025 · 1 revision

C# Basics: First Console App and Simple Input

This lesson gives you a first experience with a C# console project and basic user input.

Related wiki pages:

  • Overview: CSharpBible.md
  • Basics of Console.WriteLine and DateTime: CSharp-Basics-ConsoleWriteLine-DateTime.md

Official docs:


1. Create your first console app

From a terminal:

dotnet new console -o MyFirstApp
cd MyFirstApp

The template creates a Program.cs with a Main method.

Replace Program.cs with:

using System;

namespace MyFirstApp
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Hello, C#!");
        }
    }
}

Run it:

dotnet run

2. Basic types recap

  • int – whole numbers
  • string – text
  • booltrue/false

3. Read and validate user input

using System;

namespace MyFirstApp
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.Write("Please enter your age: ");
            string? input = Console.ReadLine();

            if (!int.TryParse(input, out int age))
            {
                Console.WriteLine("Error: please enter a whole number.");
                return;
            }

            if (age >= 18)
            {
                Console.WriteLine("You are an adult.");
            }
            else
            {
                Console.WriteLine("You are not an adult.");
            }
        }
    }
}

This combines:

  • Console.ReadLine for input
  • int.TryParse for safe conversion
  • if/else for basic branching

For more input/output patterns see Lesson20.12.2025.md (to be renamed to an I/O lesson file).


4. Next steps

  • Extend this program to be a simple calculator.
  • Combine basic types from Lesson07.12.2025.md with input/output from this lesson.

Clone this wiki locally