Getting Started with C# on macOS Using Visual Studio Code
C# is a powerful programming language developed by Microsoft that enables developers to build a variety of applications ranging from simple console apps to complex enterprise-level solutions. In this article, we’ll guide you through setting up your macOS environment for C# development using Visual Studio Code (VS Code) and then walk you through writing and executing a simple “Hello World” program.
Step 1: Install Visual Studio Code
First, you need to install Visual Studio Code, a lightweight and powerful code editor that supports a multitude of programming languages including C#. It’s widely used for its versatility, performance, and extensive plugin system.
- Go to the Visual Studio Code website.
- Download the stable build for macOS.
- Open the downloaded
.zip
file and move Visual Studio Code to your Applications folder.
Step 2: Install the .NET SDK
C# programs require the .NET SDK (Software Development Kit). For macOS, you can download it directly from Microsoft.
- Visit the .NET download page.
- Choose the SDK (Software Development Kit) for macOS.
- Follow the instructions to download and install the SDK.
After installation, you can verify that it was installed correctly by opening a Terminal and typing:
dotnet --version
This command will display the version of the .NET SDK installed on your machine.
Step 3: Install the C# Extension in VS Code
To effectively develop in C#, you should install the official C# extension from Microsoft.
- Open Visual Studio Code.
- Navigate to the Extensions view by clicking on the square icon on the sidebar or pressing
Cmd+Shift+X
. - Search for “C#”.
- Find the C# extension by Microsoft (usually the first result) and click Install.
Step 4: Create Your First C# Program
Now that you have your environment set up, you can start writing your first simple C# program.
- Open VS Code.
- Create a new folder for your project, and open it in VS Code.
- Open the Terminal in VS Code by selecting
Terminal > New Terminal
from the top menu. - In the Terminal, create a new console application by typing:
dotnet new console -n HelloWorld
- This command creates a new folder named
HelloWorld
containing a simple "Hello World" program. - Navigate to the project folder by typing:
cd HelloWorld
Step 5: Explore the Generated Code
VS Code and the .NET CLI have automatically generated a simple C# program for you. Open the Program.cs
file in your VS Code editor. It should look like this:
using System;
namespace HelloWorld
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Hello World!");
}
}
}
Step 6: Run Your Program
You’re now ready to run your first C# program.
- Ensure you are in the project’s root directory in your Terminal (inside the
HelloWorld
folder). - Type the following command to run your application:
dotnet run
This command compiles and executes your program, and you should see “Hello World!” printed in the terminal.
Conclusion
Congratulations! You have successfully set up your macOS environment for C# development and executed your first C# program using Visual Studio Code. This setup provides a robust platform for developing further more complex C# applications and exploring the rich ecosystem of .NET.