Cross-platform development - .NET Core Tutorial
Cross-platform development is becoming increasingly popular in today's software development world. Developers want to write code that can work across different platforms, including Windows, Linux, and macOS. .NET Core is a free and open-source, cross-platform, and optimized version of the .NET Framework. In this tutorial, we will explore cross-platform development using .NET Core.
Prerequisites
Before we begin, make sure that you have the following installed on your machine:
- .NET Core SDK
- An IDE of your choice (Visual Studio Code, Visual Studio, JetBrains Rider)
Creating a .NET Core Project
To create a .NET Core project, follow these steps:
Open your command prompt or terminal and create a new directory for your project.
Navigate to the directory and run the following command:
dotnet new console -n MyProject
This creates a new .NET Core console app project named
MyProject
.After the project is created, navigate to its directory and run the following command:
dotnet run
This should build and run the project, and you should see the "Hello World!" message on your console output.
Writing Cross-Platform Code
When writing cross-platform code, it is important to consider the differences in file paths, environment variables, and other system-specific factors. The .NET Core platform provides a simple way to handle these differences. You can use the Path.Combine()
method to combine file paths in a platform-independent way.
Here's an example of how to use it:
string path = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.UserProfile), "Documents", "MyFile.txt");
This creates a file path that points to MyFile.txt
in the user's "Documents" folder, regardless of which platform the code is running on.
Compiling and Running Cross-Platform Code
To compile and run your .NET Core code, you can use the dotnet
command-line tools. Here are some useful commands:
dotnet build
- This command builds the project.dotnet run
- This command builds and runs the project.dotnet test
- This command runs the tests in your project.
Using .NET Core with Docker
Docker is a platform that allows developers to build, deploy, and run applications in a containerized environment. .NET Core supports Docker, which makes it easy to create and package cross-platform applications.
Here's an example of how to use .NET Core with Docker:
Create a Dockerfile in your project directory that looks like this:
FROM mcr.microsoft.com/dotnet/core/sdk:3.1 AS build-env WORKDIR /app # Copy the project file COPY *.csproj ./ # Restore packages RUN dotnet restore # Copy the rest of the files COPY . ./ # Build the app RUN dotnet publish -c Release -o out # Build the runtime image FROM mcr.microsoft.com/dotnet/core/runtime:3.1 WORKDIR /app COPY --from=build-env /app/out ./ # Start the app ENTRYPOINT ["dotnet", "MyProject.dll"]
Build the Docker image:
docker build -t myproject .
This command builds an image from the Dockerfile in your project directory and tags it as
myproject
.Run the Docker container:
docker run --rm myproject
This command runs a container from the
myproject
image and removes the container automatically after the program has run.
Conclusion
In this tutorial, we learned how to create and run a .NET Core project, write cross-platform code, and use .NET Core with Docker. With .NET Core, you can write code that works seamlessly across different platforms and environments.