Just a warning, this question requires a preview of .NET 6 to be installed
I'm trying to create an interface in C# that can allow the +
operator similar to how it is implemented in Microsoft's INumber<T>
.
Interfaces.cs
using System;
using System.Runtime.Versioning;
namespace InterfaceTest;
[RequiresPreviewFeatures]
public interface IExtendedArray<T> : IAdditionOperators<IExtendedArray<T>, IExtendedArray<T>, IExtendedArray<T>>
where T : INumber<T>
{
Array Data { get; }
}
[RequiresPreviewFeatures]
public interface IExtendedFloatingPointArray<T> : IExtendedArray<T>
where T : IFloatingPoint<T>
{
}
InterfaceTest.csproj
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<EnablePreviewFeatures>true</EnablePreviewFeatures>
<TargetFramework>net6.0</TargetFramework>
<ImplicitUsings>false</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="System.Runtime.Experimental" Version="6.0.0-preview.7.21377.19" />
</ItemGroup>
</Project>
However, this code produces
error CS8920: The interface 'InterfaceTest.IExtendedArray' cannot be used as type parameter 'TSelf' in the generic type or method 'IAdditionOperators<TSelf, TOther, TResult>'. The constraint interface 'System.IAdditionOperators<InterfaceTest.IExtendedArray, InterfaceTest.IExtendedArray, InterfaceTest.IExtendedArray>' or its base interface has static abstract members.
Is there a way achieve this with custom types?
dotnet --list-sdks
shows I have 6.0.100-rc.1.21458.32
installed. But I just had it installed through the Visual Studio 2022 Preview 4.