4 January 2017
Now that you’ve successfully set up your .NET Core development environment on macOS, it’s time to dive into creating your first .NET Core application. In this post, we will build a simple “Hello World” console application, and I’ll walk you through the basics of the Program.cs file and the Main() method.
When I first started exploring .NET Core, I was amazed at how easy it was to create a project and get it up and running. Let’s do that together by building a very basic app that will print “Hello World” to the terminal.
1. Creating the Application
Start by opening your terminal, navigating to the folder where you want to create your application, and running the following command to generate a new console application:
dotnet new console -n HelloWorldApp
This will create a new folder called HelloWorldApp
, containing the necessary files for your first console application.
2. Navigating to Your Project Folder
Once the project is created, navigate to the folder by typing:
cd HelloWorldApp
Now you’re inside the project folder, where all your code and dependencies will live.
3. Exploring the Program.cs File
Inside your project folder, you’ll notice a file named Program.cs. This is where your application’s execution starts. Open this file in Visual Studio Code or any text editor of your choice, and you’ll see something like this:
using System;
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Hello World!");
}
}
Let’s break this down:
- using System;: This line imports the
System
namespace, which contains fundamental classes and methods, such asConsole
—the class we’ll use to interact with the terminal. - class Program: This declares a class called
Program
. In .NET Core, classes are containers for methods and logic. TheProgram
class is typically used to house your main entry point. - static void Main(string[] args): This is the entry point of your application. The
Main()
method is where execution starts. It’s the method that gets called when you run your program.string[] args
allows you to pass in command-line arguments if needed, but for now, we won’t worry about that. - Console.WriteLine(“Hello World!”);: This line prints the text “Hello World!” to the terminal using the
Console.WriteLine
method.
4. Running the Application
Now that we’ve explored the code, it’s time to run the application. Go back to your terminal and run the following command:
dotnet run
You should see the output:
Hello World!
Congratulations! You’ve just built and run your first .NET Core application.
5. What’s Happening Under the Hood?
When you run the dotnet run
command, the .NET Core CLI compiles the code and then executes the program. The dotnet
command also restores any required packages (if applicable), builds the app, and runs it in one go, which is pretty convenient.
6. Next Steps
While the “Hello World” app is a simple example, this is the foundation for more complex .NET Core applications. In future posts, we’ll dive deeper into more advanced topics, such as working with user input, adding dependencies, and building libraries. For now, it’s important to become comfortable with how the basic structure works.