Create Your First .NET Application

Introduction

Welcome to the .NET ecosystem! This tutorial will guide you through the essential steps of creating a simple application using .NET. Whether you're building web applications, desktop software, or mobile experiences, .NET provides a powerful and versatile platform.

We'll focus on creating a basic console application, which is a great starting point to understand fundamental concepts like project structure, code compilation, and basic output.

Prerequisites

Before you begin, ensure you have the following installed:

  • .NET SDK: Download and install the latest .NET SDK from the official dotnet.microsoft.com/download website.
  • Code Editor: A code editor such as Visual Studio Code, Visual Studio, or JetBrains Rider.
Note: Make sure to select the correct SDK version (e.g., .NET 6, .NET 7, .NET 8) during installation. For this tutorial, we'll assume you're using a recent LTS (Long-Term Support) version.

Step 1: Create a New Project

Open your terminal or command prompt and navigate to the directory where you want to create your project. Then, run the following command:

dotnet new console -o MyFirstApp

This command does the following:

  • dotnet new console: Tells the .NET CLI to create a new project using the built-in 'console' template.
  • -o MyFirstApp: Specifies that the new project files should be placed in a directory named MyFirstApp.

After running this command, a new folder named MyFirstApp will be created, containing the basic files for your console application.

Step 2: Explore the Project Files

Navigate into the newly created project directory:

cd MyFirstApp

Inside the MyFirstApp folder, you'll find:

  • MyFirstApp.csproj: The project file that contains metadata about your application, such as its name, target framework, and dependencies.
  • Program.cs: The main code file for your application. This is where you'll write your C# code.
  • obj directory: Contains intermediate files generated during the build process.
Tip: Open the MyFirstApp folder in your code editor to see the project structure.

Step 3: Write Your First Code

Open the Program.cs file in your code editor. You'll see some default code. Let's modify it to display a personalized greeting:

// Program.cs
                using System;

                Console.WriteLine("Welcome to .NET!");
                Console.Write("What's your name? ");
                string? name = Console.ReadLine();
                Console.WriteLine($"Hello, {name}! Your first .NET app is running.");
                

Explanation of the code:

  • using System;: Imports the System namespace, which contains fundamental classes like Console.
  • Console.WriteLine(...): Writes a line of text to the console and moves to the next line.
  • Console.Write(...): Writes text to the console without moving to the next line.
  • string? name = Console.ReadLine();: Reads a line of text entered by the user and stores it in the name variable. The ? indicates that the string can be null.
  • Console.WriteLine($"Hello, {name}! Your first .NET app is running.");: Uses string interpolation (the $ prefix) to embed the value of the name variable directly into the string.

Step 4: Run Your Application

Save the changes to Program.cs. Now, run your application from the terminal within the MyFirstApp directory using the following command:

dotnet run

The .NET CLI will compile your code and then execute it. You should see output similar to this:

Welcome to .NET!
                What's your name? [You type your name here and press Enter]
                Hello, [Your Name]! Your first .NET app is running.
Note: The first time you run dotnet run, it might take a moment as it restores dependencies and builds the project.

Next Steps

Congratulations! You've successfully created and run your first .NET application. From here, you can explore more advanced concepts:

  • Web Applications: Learn about ASP.NET Core.
  • Desktop Applications: Explore Windows Forms or WPF.
  • Data Access: Discover Entity Framework Core.
  • APIs: Build RESTful APIs with minimal code.

Continue your learning journey by exploring other tutorials and the comprehensive API documentation available on MSDN.