How to Create a C# Console Application with .NET

Learn how to create a C# console app with .NET using the CLI, user input, conditions, methods, build commands and a simple calculator example.

A console application is one of the easiest ways to start learning C# and .NET. It runs in a terminal or command prompt, which allows you to focus on programming concepts without first building a graphical user interface.

In this tutorial, you'll create a C# console application using the .NET command-line interface (CLI), run it, accept user input, work with conditions and methods, and build a small calculator.

What you'll learn:
  • How to check whether .NET is installed
  • How to create a console project
  • How to run a .NET application
  • How Program.cs works
  • How to accept user input
  • How to use conditions and methods
  • How to build a simple calculator

What Is a .NET Console Application?

A console application is a program that normally interacts with users through text in a terminal instead of through windows, buttons, and other graphical controls.

Console applications are useful for learning C#, but they are not limited to beginner exercises. They can also be used for command-line tools, automation, data-processing jobs, utilities, scheduled tasks, and other background operations.

.NET Core vs .NET: What's the Difference?

If you've followed older tutorials, you may have seen the name .NET Core.

.NET Core was Microsoft's cross-platform implementation of .NET. Starting with .NET 5, Microsoft dropped the word "Core" from the main product name and continued the platform as .NET.

That's why modern tutorials generally say .NET 8, .NET 9, .NET 10, and so on rather than ".NET Core 8" or ".NET Core 9".

The command-line concepts in this tutorial are the modern continuation of the .NET Core tooling you may recognize from older tutorials.

Prerequisites

Before starting, you need:

  • A supported .NET SDK
  • A terminal or command prompt
  • A code editor such as Visual Studio or Visual Studio Code

After installing the .NET SDK, open a terminal and run:

dotnet --version

If .NET is installed correctly, this command displays the installed SDK version.

Step 1: Create a New Console Application

Open your terminal and navigate to the folder where you want to create the project.

Run:

dotnet new console -n MyConsoleApp

This command tells the .NET CLI to create a new project using the console template and name it MyConsoleApp.

Move into the project directory:

cd MyConsoleApp

Step 2: Understand the Generated Project

Inside the project folder, you'll see files including:

MyConsoleApp/
├── MyConsoleApp.csproj
└── Program.cs

MyConsoleApp.csproj

The .csproj file contains project configuration such as the target framework and other project settings.

A modern console project may contain configuration similar to:

<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>net10.0</TargetFramework>
    <ImplicitUsings>enable</ImplicitUsings>
    <Nullable>enable</Nullable>
  </PropertyGroup>

</Project>

Your TargetFramework value depends on the .NET SDK and template version you're using.

Program.cs

Program.cs contains the code that runs when the application starts.

A newly generated modern console application can be very small:

Console.WriteLine("Hello, World!");

You may wonder where Main() went.

Modern C# supports top-level statements. This allows simple applications to omit the explicit Program class and Main() method.

Step 3: Run the Console Application

From the project directory, run:

dotnet run

You should see:

Hello, World!

The .NET CLI builds the project and runs the resulting application.

Step 4: Change the Program

Open Program.cs and replace the existing code with:

Console.WriteLine("Welcome to JaspTech Pulse!");
Console.WriteLine("My first C# console application is running.");

Run it again:

dotnet run

The console should display:

Welcome to JaspTech Pulse!
My first C# console application is running.

Step 5: Accept User Input

Most useful applications need to receive information from a user or another source.

We can use Console.ReadLine() to read text entered into the console:

Console.Write("Enter your name: ");

string? name = Console.ReadLine();

Console.WriteLine($"Hello, {name}!");

If the user enters:

Alex

The program displays:

Hello, Alex!

Step 6: Work with Numbers

Console.ReadLine() returns text. If we want the user to enter a number, we need to convert or parse that text.

A safe beginner-friendly approach is int.TryParse():

Console.Write("Enter your age: ");

string? input = Console.ReadLine();

if (int.TryParse(input, out int age))
{
    Console.WriteLine($"Your age is {age}.");
}
else
{
    Console.WriteLine("Please enter a valid number.");
}

Unlike directly converting arbitrary input, TryParse() allows us to handle invalid input without an exception being thrown for a normal validation failure.

Step 7: Add Conditions

Conditions allow the application to make decisions.

Console.Write("Enter your age: ");

string? input = Console.ReadLine();

if (int.TryParse(input, out int age))
{
    if (age >= 18)
    {
        Console.WriteLine("You are 18 or older.");
    }
    else
    {
        Console.WriteLine("You are under 18.");
    }
}
else
{
    Console.WriteLine("Invalid age.");
}

The if statement checks whether a condition is true and runs the corresponding code.

Step 8: Use a Loop

Loops allow us to execute code repeatedly.

for (int i = 1; i <= 5; i++)
{
    Console.WriteLine($"Count: {i}");
}

The output is:

Count: 1
Count: 2
Count: 3
Count: 4
Count: 5

Step 9: Create a Method

Methods help organize code into reusable pieces.

static int Add(int firstNumber, int secondNumber)
{
    return firstNumber + secondNumber;
}

int result = Add(10, 20);

Console.WriteLine($"Result: {result}");

The result is:

Result: 30

The Add() method receives two integers and returns their sum.

Step 10: Build a Simple Console Calculator

Now let's combine several concepts into a small project.

Console.Write("Enter first number: ");

if (!double.TryParse(Console.ReadLine(), out double firstNumber))
{
    Console.WriteLine("Invalid first number.");
    return;
}

Console.Write("Enter second number: ");

if (!double.TryParse(Console.ReadLine(), out double secondNumber))
{
    Console.WriteLine("Invalid second number.");
    return;
}

Console.Write("Choose an operation (+, -, *, /): ");

string? operation = Console.ReadLine();

double result;

switch (operation)
{
    case "+":
        result = firstNumber + secondNumber;
        break;

    case "-":
        result = firstNumber - secondNumber;
        break;

    case "*":
        result = firstNumber * secondNumber;
        break;

    case "/":
        if (secondNumber == 0)
        {
            Console.WriteLine("Cannot divide by zero.");
            return;
        }

        result = firstNumber / secondNumber;
        break;

    default:
        Console.WriteLine("Invalid operation.");
        return;
}

Console.WriteLine($"Result: {result}");

This small application uses several important C# concepts:

  • Variables
  • User input
  • Numeric parsing
  • Conditions
  • A switch statement
  • Basic error handling
  • Console output

Build vs Run in .NET

Two commands you'll use frequently are dotnet build and dotnet run.

Build the Application

dotnet build

This compiles the project and reports compilation errors without requiring you to manually start the resulting executable.

Run the Application

dotnet run

This builds the project when necessary and runs the application.

How to Pass Command-Line Arguments

Console applications can also receive arguments when they are started.

For example, change Program.cs to:

if (args.Length == 0)
{
    Console.WriteLine("No arguments supplied.");
    return;
}

Console.WriteLine("Arguments:");

foreach (string argument in args)
{
    Console.WriteLine(argument);
}

You can run it with arguments using:

dotnet run -- apple banana orange

The output will be similar to:

Arguments:
apple
banana
orange

How to Publish a Console Application

During development, dotnet run is convenient. When you're ready to distribute an application, you can use the publish command.

dotnet publish -c Release

This creates publish output using the Release configuration.

.NET also supports different deployment options, including framework-dependent and self-contained deployments. The right option depends on where and how the application will run.

Common Problems

'dotnet' Is Not Recognized

If your terminal says that dotnet is not recognized or cannot be found, check that the .NET SDK is installed and that the installation location is available through your system's PATH.

Close and reopen your terminal after installing the SDK.

SDK vs Runtime

The .NET Runtime allows compatible .NET applications to run. The .NET SDK includes the development tools required to create, build, and publish applications.

If you're developing C# applications, install the appropriate SDK.

Build Errors

Run:

dotnet build

Read the error message carefully. Compiler errors normally include information about the file and location where the compiler detected the problem.

Program Closes Immediately

If you launch a console executable directly and its work finishes immediately, the console window may disappear before you can read the output.

During development, run the program from an existing terminal so that you can see its output and any error messages.

What Should You Learn Next?

After you're comfortable creating and running console applications, continue with core C# concepts such as:

  1. Variables and data types
  2. Conditions
  3. Loops
  4. Methods
  5. Arrays and collections
  6. Classes and objects
  7. Exception handling
  8. LINQ
  9. Asynchronous programming
  10. Dependency injection

If your goal is web/backend development, you can then progress into ASP.NET Core, Web APIs, Entity Framework Core, authentication, testing, and deployment.

Frequently Asked Questions

Is .NET Core still used?

The technology continues as modern .NET, but the main product name changed after .NET Core 3.1. Modern releases use names such as .NET 8, .NET 9 and .NET 10 rather than ".NET Core 8" or ".NET Core 9".

Is C# the same as .NET?

No. C# is a programming language. .NET is a development platform that provides a runtime, libraries, SDK, tooling, and other components used to build and run applications. C# is one of the primary languages used with .NET.

What is Program.cs?

Program.cs is conventionally the source file containing the starting code for a C# application. Modern C# console templates use top-level statements, so an explicit Main() method may not appear in the source code.

Do I need Visual Studio to create a .NET console app?

No. You can create, build, and run .NET projects using the .NET CLI and a text editor or another compatible development environment. Visual Studio is useful, but it is not required for this example.

What does dotnet run do?

dotnet run provides a convenient way to run a .NET project from its source during development. It builds the application when necessary and starts it.

Is a console application useful for beginners?

Yes. Console applications let beginners concentrate on C# syntax, programming logic, debugging, and application structure without also having to learn a graphical user-interface framework.

Conclusion

You've now created a C# console application with .NET and learned how to run it using the .NET CLI.

We also went beyond "Hello World" by accepting user input, validating numbers, using conditions, creating methods, passing command-line arguments, and building a small calculator.

Console applications are a good environment for strengthening your C# fundamentals. Once you're comfortable solving small problems in C#, you'll be in a much better position to move into larger technologies such as ASP.NET Core Web APIs.

Hello! My name is Aniket Shahane and I am a senior software consultant. I hold a post-graduate degree (MTech) in Computer Science and Engineering, and I have a passion for using my technical expertise to solve complex problems. I am excited to be here and eager to share my knowledge and experience with you.

Post a Comment