net-standard
  1. net-standard-working-with-platform-specific-apis

Working with Platform-Specific APIs - (.NET Standard API Surface)

.NET Standard is a set of APIs that provide a consistent set of functionality across different platforms, including Windows, Linux, and macOS. However, there may be times when you need to use platform-specific functionality that is not available in .NET Standard. In this tutorial, we'll discuss best practices for working with platform-specific APIs in .NET Standard.

Syntax

Working with platform-specific APIs in .NET Standard involves using conditional compilation to include platform-specific code. The syntax for conditional compilation in C# is as follows:

#if __ANDROID__
    // Android-specific functionality
#elif __IOS__
    // iOS-specific functionality
#elif __MACOS__
    // macOS-specific functionality
#elif NETSTANDARD1_6
    // .NET Standard 1.6-specific functionality
#else
    // Other platforms
#endif

Example

To illustrate working with platform-specific APIs in .NET Standard, let's look at an example. Suppose we want to check the battery level of a mobile device. This functionality is available on Android and iOS, but not on other platforms.

public int GetBatteryLevel()
{
    int batteryLevel = -1;

#if __ANDROID__
    using (var batteryManager = (BatteryManager)context.GetSystemService(Context.BatteryService))
    {
        batteryLevel = batteryManager.GetIntProperty(BatteryProperty.Capacity);
    }
#elif __IOS__
    batteryLevel = UIDevice.CurrentDevice.BatteryLevel;
    batteryLevel = (int)Math.Floor(batteryLevel * 100);
#endif

    return batteryLevel;
}

In this code, we're checking the platform and then calling the appropriate code to retrieve the battery level. For Android, we're using the BatteryManager API to retrieve the battery level, while for iOS, we're using the UIDevice API.

Explanation

Working with platform-specific APIs in .NET Standard requires using conditional compilation to include platform-specific code. This allows you to use platform-specific functionality where it's available and fallback to other implementations for other platforms.

Use

Working with platform-specific APIs in .NET Standard is useful when you need to use platform-specific functionality that is not available in .NET Standard. By using conditional compilation, you can write platform-specific code that works across platforms.

Important Points

Here are some important points to keep in mind when working with platform-specific APIs in .NET Standard:

  • Always check if the platform-specific API is available before using it.
  • Use conditional compilation to include platform-specific code.
  • Keep platform-specific code in separate files or projects to simplify maintenance.

Summary

In this tutorial, we discussed best practices for working with platform-specific APIs in .NET Standard. We covered the syntax, example, explanation, use, and important points of working with platform-specific APIs in .NET Standard. With this knowledge, you can use platform-specific functionality where it's available and fallback to other implementations for other platforms.

Published on: