net-standard
  1. net-standard-sharing-code-across-platforms

Sharing Code Across Platforms - (.NET Standard Multi-Targeted Libraries)

As a developer, you may be working on projects that target multiple platforms, such as Windows, Android, iOS, or Linux. To avoid duplicating code across each platform, you can use a common code base that can be shared across multiple platforms. This is where .NET Standard multi-targeted libraries come in. In this tutorial, we'll explore .NET Standard and how to create multi-targeted libraries that can be shared across platforms.

Syntax

In Visual Studio, to create a multi-targeted library that targets multiple platforms, follow these steps:

  1. Create a new class library project.
  2. In the project properties, select "Target frameworks".
  3. Select ".NET Standard" and a specific version (e.g. .NET Standard 2.0).
  4. Add your code files to the project.

Example

To create a simple .NET Standard multi-targeted library, create a new class library project in Visual Studio, and select ".NET Standard" as the target framework. Then, add the following code:

using System;

namespace MyLibrary {
    public class MyLibraryClass {
        public static void SayHello() {
            Console.WriteLine("Hello from MyLibrary!");
        }
    }
}

You can then reference this library in multiple projects, such as a console app, a web app, and a mobile app.

Explanation

.NET Standard is a version-independent API that defines a set of common APIs for .NET platforms. By creating a multi-targeted library that targets .NET Standard, you can create a common code base that can be shared across multiple platforms, such as Windows, Linux, macOS, Android, and iOS.

.NET Standard libraries can be used in multiple .NET implementations, including .NET Core, .NET Framework, Xamarin, Unity, and more. By targeting a specific version of .NET Standard that supports the platforms you're targeting, you can create a library that can be used in any project that targets those platforms.

Use

Creating a .NET Standard multi-targeted library allows you to share code across multiple platforms, which can save time and reduce duplication of code. This is especially useful if you have platform-specific code that needs to be shared across multiple projects.

Important Points

Here are some important points to keep in mind when using .NET Standard multi-targeted libraries:

  • Choose the lowest version of .NET Standard that supports the platforms you're targeting.
  • Keep platform-specific code in separate files or use preprocessor directives to include/exclude code.
  • Test your library on each platform you're targeting to ensure compatibility.

Summary

In this tutorial, we explored the benefits of using .NET Standard multi-targeted libraries to share code across multiple platforms. We covered the syntax, example, explanation, use, and important points of creating a .NET Standard library. With this knowledge, you can create multi-targeted libraries that can be shared across platforms, saving time and reducing duplication of code.

Published on: