Creating Your First Console Application

Beginner 15 min read Lesson 1 of 4

What is a Console Application?

A console application is a program that runs in a command-line interface (terminal/command prompt). It's the simplest type of application you can build in C# and is perfect for learning the fundamentals.

Creating Your First Project

Method 1: Using Visual Studio

1Open Visual Studio

Launch Visual Studio and click "Create a new project".

2Select Template

Choose "Console App (.NET Core)" or "Console App (.NET Framework)"

3Configure Project

Set Project Name: "MyConsoleApp", Location, and click "Create".

Method 2: Using Command Line

dotnet new console -n MyConsoleApp
cd MyConsoleApp
dotnet run

Understanding the Project Structure

MyConsoleApp/
├── Program.cs          // Main program file
├── MyConsoleApp.csproj // Project configuration
├── obj/               // Build objects
└── bin/               // Compiled binaries

The Program.cs File

Here's what the default Program.cs looks like:

using System;

namespace MyConsoleApp
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Hello World!");
        }
    }
}

Breaking Down the Code

  • using System; - Imports the System namespace
  • namespace MyConsoleApp - Defines a namespace
  • class Program - The main class
  • static void Main(string[] args) - Entry point of the application
  • Console.WriteLine - Outputs text to the console
Key Takeaway

Every console application starts with the Main method. This is where your program begins execution.

Running Your Application

  • Visual Studio: Press F5 or click "Start" button
  • Command Line: Navigate to project folder and run dotnet run
Exercise

Create a console application that:

  1. Prints "Welcome to C#!"
  2. Prints your name on a new line
  3. Prints "Press any key to exit..." and waits for input
Test Your Knowledge - Take Quiz