1

With the creation of C# 11, static abstract operators are now allowed to be defined in interfaces. Because of this, the .NET Core 7 library now includes the interfaces System.Numerics.IEqualityOperators and System.Numerics.IComparisonOperators.

The IComparisonOperators interface is defined somewhat as I would expect:

namespace System.Numerics;
public interface IComparisonOperators<TSelf, TOther, TResult>
    : IEqualityOperators<TSelf, TOther, TResult>
    where TSelf : IComparisonOperators<TSelf, TOther, TResult>?
{
    static abstract TResult operator <(TSelf left, TOther right);
    static abstract TResult operator >(TSelf left, TOther right);
    static abstract TResult operator <=(TSelf left, TOther right);
    static abstract TResult operator >=(TSelf left, TOther right);
}

However, take a look at the IEqualityOperators interface:

namespace System.Numerics;
public interface IEqualityOperators<TSelf, TOther, TResult>
    where TSelf : IEqualityOperators<TSelf, TOther, TResult>?
{
    static abstract TResult operator ==(TSelf? left, TOther? right);
    static abstract TResult operator !=(TSelf? left, TOther? right);
}

The parameters to the two operators are suddenly nullable! Why would the equality operators be nullable if the comparison operators were not?

Additionally, in both interfaces, the ? operator is also used in the type constraints. I know that T? is equivalent to Nullable<T>, but cannot understand it in the context of type constraints. Is it requiring TSelf to inherit from System.Nullable? I wasn't even aware that was possible!

Any explanation would be greatly appreciated!

256Bits
  • 378
  • 1
  • 13

0 Answers0