Search code examples
c#.net.net-6.0preview-feature.net-generic-math

How can I use IAdditionOperators<,,> from .NET 6 preview 7?


I have installed .NET 6 Preview 7 successfully. I have access to System.IAdditionOperators<,,>. My problem is that when I try to implement the interface it doesn't require me to define the + operator.

My project file:

<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <TargetFramework>net6.0</TargetFramework>
  </PropertyGroup>
  <PropertyGroup>
    <EnablePreviewFeatures>true</EnablePreviewFeatures>
    <LangVersion>preview</LangVersion>
    <TargetFramework>net6.0</TargetFramework>
  </PropertyGroup>
  <ItemGroup>
    <PackageReference Include="System.Runtime.Experimental" Version="6.0.0-preview.7.21377.19" />
  </ItemGroup>

</Project>

My one and only class:

using System;

namespace TestCode
{
    public class Foo:IAdditionOperators<Foo,Foo,Foo>
    {
    }
}

The code compiles (which it shouldn't!) even though I haven't implemented IAdditionOperators<Foo,Foo,Foo>. How do I make it force me to implement the + operator?

I have already downloaded the SDK, and updated Visual Studio to version 16.11.4. I have already checked the box for "Use preview of the .NET SDK" and restarted Visual Studio. What else do I have to do?


Solution

  • To make the compilation fail you need either to use dotnet build from terminal or install VS 2022 Preview (I had the same problem for this answer). As for the code compiled by VS 2019 - it should fail in runtime when you try to start your application.