Creating your first .NET Console Application is a fundamental step toward mastering the .NET development ecosystem. A console app is one of the simplest types of applications you can build using .NET and is ideal for learning programming fundamentals, exploring the .NET CLI, and testing new features. In this guide, weβll take you through a step-by-step tutorial to build, run, and understand your first .NET Console Application using C# and the .NET SDK.
A .NET Console Application is a lightweight program that runs in a terminal or command prompt window. It does not include any graphical user interface and is typically used for simple utilities, automation scripts, or educational purposes.
Console apps are ideal for:
Before you begin, make sure you have the following tools installed on your system:
You need the latest .NET SDK (Software Development Kit). You can download it from the official site: https://dotnet.microsoft.com/download
You can use:
Youβll need a terminal (Command Prompt, PowerShell, Terminal, etc.) to execute .NET CLI commands.
Use the following command to create a new console application in .NET:
dotnet new console -n MyFirstDotNetApp
This will:
cd MyFirstDotNetApp
Open Program.cs file. You will see something like this:
using System;
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Hello, World!");
}
}
This program does the following:
dotnet run
You should see the output:
Hello, World!
Letβs modify the program to accept user input and display a personalized message.
using System;
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Enter your name:");
string name = Console.ReadLine();
Console.WriteLine($"Welcome to .NET, {name}!");
}
}
This version introduces:
The .csproj file contains metadata about the project including SDK version, target framework, and dependencies.
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net7.0</TargetFramework>
</PropertyGroup>
</Project>
Letβs separate our logic into a new class file. Create a new file named Greeter.cs:
public class Greeter
{
public string Greet(string name)
{
return $"Hello, {name}! Welcome to the world of .NET!";
}
}
Now modify the Program.cs to use the new class:
using System;
class Program
{
static void Main(string[] args)
{
Console.Write("Enter your name: ");
string name = Console.ReadLine();
Greeter greeter = new Greeter();
string message = greeter.Greet(name);
Console.WriteLine(message);
}
}
dotnet run
You'll now see a personalized greeting using your newly created class.
dotnet new console -n AppNamedotnet restoredotnet builddotnet rundotnet publish -c Release -o ./publishIf you're using Visual Studio or VS Code, you can set breakpoints, inspect variables, and step through your code. For command-line debugging, you can use:
dotnet build
dotnet run --configuration Debug
try
{
Console.Write("Enter a number: ");
int number = Convert.ToInt32(Console.ReadLine());
Console.WriteLine($"Square: {number * number}");
}
catch (FormatException)
{
Console.WriteLine("Please enter a valid number.");
}
static void Main(string[] args)
{
if (args.Length > 0)
{
Console.WriteLine("Argument: " + args[0]);
}
else
{
Console.WriteLine("No arguments passed.");
}
}
for (int i = 1; i <= 5; i++)
{
Console.WriteLine($"Count: {i}");
}Congratulations! You've successfully created and customized your first .NET Console Application. This foundational step equips you with the understanding of project structure, syntax, .NET CLI, and best practices. Whether you're a beginner or returning developer, console applications are a perfect playground for mastering .NET and C#. As you grow your knowledge, youβll be ready to build web APIs, desktop applications, mobile apps, and cloud-native solutions using the versatile .NET ecosystem.
Copyrights © 2024 letsupdateskills All rights reserved