c#-7.3

Why is a generic type constrained by 'Enum' failing to qualify as a 'struct' in C# 7.3?

荒凉一梦 提交于 2019-11-27 23:45:41
问题 If I have a generic interface with a struct constraint like this: public interface IStruct<T> where T : struct { } I can supply an enumeration as my type T like so, because an enum satisfies a struct constraint: public class EnumIsAStruct : IStruct<DateTimeKind> { } C# 7.3 added an Enum constraint. The following code, which was previously illegal, now compiles: public class MCVE<T> : IStruct<T> where T : struct, Enum { } However, to my surprise, the following fails to compile: public class

Equality and polymorphism

烂漫一生 提交于 2019-11-27 15:39:55
问题 With two immutable classes Base and Derived (which derives from Base) I want to define Equality so that equality is always polymorphic - that is ((Base)derived1).Equals((Base)derived2) will call Derived.Equals operators == and != will call Equals rather than ReferenceEquals (value equality) What I did: class Base: IEquatable<Base> { public readonly ImmutableType1 X; readonly ImmutableType2 Y; public Base(ImmutableType1 X, ImmutableType2 Y) { this.X = X; this.Y = Y; } public override bool