net-standard
  1. net-standard-conditional-compilation

Conditional Compilation - (.NET Standard Multi-Targeted Libraries)

Conditional Compilation is a powerful feature that allows you to include or exclude specific sections of code depending on the target platform. In .NET Standard Multi-Targeted Libraries, it can be used to target specific platforms and frameworks. In this tutorial, we'll discuss what conditional compilation is and how to use it in .NET Standard Multi-Targeted Libraries.

Syntax

Conditional Compilation in .NET Standard Multi-Targeted Libraries is achieved using compiler directives. A compiler directive is a special instruction that tells the compiler to include or exclude specific sections of code based on certain conditions.

#if condition1
    // code to conditionally compile for condition1
#elif condition2
    // code to conditionally compile for condition2
#else
    // default code to compile if none of the conditions are met
#endif

Example

Let's say we have a .NET Standard Multi-Targeted Library project that targets both .NET Standard 2.0 and .NET Framework 4.5. We want to include a section of code for the .NET Framework target only. We can achieve this using conditional compilation as follows:

#if NET45
    // code to compile only for .NET Framework 4.5
#endif

Here, we are using the predefined symbol NET45 to conditionally compile the code only for the .NET Framework 4.5 target.

Explanation

Conditional Compilation in .NET Standard Multi-Targeted Libraries allows you to include or exclude specific sections of code based on the target platform. This can be useful when working with multiple platforms or frameworks and you need to target specific ones.

By using compiler directives such as #if, #elif, and #else, you can conditionally compile sections of code based on a wide range of conditions, including target framework, operating system, and processor architecture.

Use

Conditional Compilation in .NET Standard Multi-Targeted Libraries is useful when you need to target specific platforms and frameworks. By using compiler directives, you can conditionally compile sections of code based on the target platform, which can help to reduce code complexity and improve performance.

Important Points

Here are some important points to keep in mind when using Conditional Compilation in .NET Standard Multi-Targeted Libraries:

  • Predefined symbols such as NET45, NETCOREAPP2_1, and NETSTANDARD2_0 can be used to reference specific target frameworks.
  • Conditional Compilation can be useful when working with multiple target platforms, but it can also lead to code complexity if not used carefully.
  • Conditional Compilation can be used in conjunction with dependency injection and other techniques to achieve maximum portability and code reuse.

Summary

In this tutorial, we discussed Conditional Compilation in .NET Standard Multi-Targeted Libraries. We covered the syntax, example, explanation, use and important points of using conditional compilation. By using Conditional Compilation, you can target specific platforms and frameworks while reducing code complexity and improving performance.

Published on: