Welcome to .NET Development!
This guide will walk you through the essential steps to build and run your first .NET application. .NET is a versatile, open-source platform for building all kinds of applications, from web and cloud services to desktop and IoT applications.
Prerequisites
Before you begin, ensure you have the following installed:
- .NET SDK: The .NET Software Development Kit includes the tools and libraries you need to develop .NET applications. You can download the latest version from the official .NET website.
- Code Editor: While any text editor will work, we recommend using Visual Studio Code with the C# extension for a rich development experience. Visual Studio is also an excellent choice for Windows development.
Step 1: Create a New Project
You can create a new .NET project using the command line interface (CLI) provided by the .NET SDK. Open your terminal or command prompt, navigate to the directory where you want to create your project, and run the following command:
dotnet new console -o MyFirstApp
This command does the following:
dotnet new console
: Creates a new console application project.-o MyFirstApp
: Specifies the output directory name for your project asMyFirstApp
.
Step 2: Navigate to Your Project Directory
Change your current directory to the newly created project folder:
cd MyFirstApp
Step 3: Explore the Project Files
Inside the MyFirstApp
directory, you'll find a few key files:
MyFirstApp.csproj
: The project file that contains project configuration, dependencies, and build settings.Program.cs
: The main source code file where your application logic resides.obj
andbin
folders: These are generated during the build process and contain intermediate and compiled output, respectively.
Step 4: Write Your Code
Open the Program.cs
file in your code editor. By default, it contains a simple "Hello, World!" program:
using System;
namespace MyFirstApp
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Hello, World!");
}
}
}
Feel free to modify the message or add more lines of code to experiment!
Step 5: Build and Run Your Application
You can build and run your application directly from the command line within your project directory:
dotnet run
The dotnet run
command first builds your project (if necessary) and then executes the compiled output. You should see the output from your application displayed in the terminal.
Troubleshooting Common Issues
- SDK not found: Ensure the .NET SDK is installed and its path is correctly configured in your system's environment variables.
- Build errors: Carefully read the error messages provided by the compiler. Syntax errors or missing references are common culprits.
- Dependencies: For projects with external libraries, ensure you restore them using
dotnet restore
.