Sdk: Package authors can use #if to perform version checks

Created on 6 Nov 2020  路  8Comments  路  Source: dotnet/sdk

0 User Story

All 8 comments

This snippet is very confusing to me:

void M()
{
    #if NETFRAMEWORK461_OR_GREATER
        LegacyNetFxBehavior();
    #elif NETCOREAPP3_1_OR_GREATER
        LegacyNetCoreBehavior();
    #elif NET6_0_OR_GREATER
        Net6OrHigherBehavior();
    #else
        #error Unhandled TFM
    #endif
}

It seems NETCOREAPP3_1_OR_GREATER means ".NET Core 3.1 or .NET 5 but not .NET 6"? This seems very strange. .NET 6 is ".NET Core 3.1 or later" from how I am looking at it.

I would have expected it to be written like this, so the highest version check comes first.

void M()
{
    #if NET6_0_OR_GREATER
        Net6OrHigherBehavior();
    #elif NETCOREAPP3_1_OR_GREATER
        LegacyNetCoreBehavior();
    #elif NETFRAMEWORK461_OR_GREATER
        LegacyNetFxBehavior();
    #else
        #error Unhandled TFM
    #endif
}

It seems NETCOREAPP3_1_OR_GREATER means ".NET Core 3.1 or .NET 5 but not .NET 6"? This seems very strange. .NET 6 is ".NET Core 3.1 or later" from how I am looking at it.

You're totally correct. The order in my if-statements is totally wrong.

@omajid You may want to read the spec now and leave feedback there.

From a quick look, the new spec looks good. Thanks for fixing it so quickly!

Wait to implement until https://github.com/dotnet/designs/pull/165 has been decided on or not.

Based on community feedback I decided to abandon that PR and leave it as OR_GREATER. @sfoslund, you're good to go!

Fixed by #14798, merged into 5.0.2xx

I want to conditionally implement IAsyncDisposable when available. Am I correct to assume that this is the right way of going about it?

#if NETSTANDARD2_1 || NETCOREAPP3_0 || NETCOREAPP3_1 || NET5_0 || NETCOREAPP3_0_OR_GREATER
    public partial class MyClass : IAsyncDisposable
    {
        public async ValueTask DisposeAsync()
        {
            // impl
        }
    }
#endif

My thinking on this is that .NET Core 3.0 and 3.1 and .NET 5 didn't actually ship with this change, so people may have an SDK that doesn't support it.

Was this page helpful?
0 / 5 - 0 ratings