c#-7.3

Deconstruction is ambiguous

为君一笑 提交于 2020-02-23 08:43:47
问题 I have a vector class with two deconstruction methods as follows: public readonly struct Vector2 { public readonly double X, Y; ... public void Deconstruct( out double x, out double y ) { x = this.X; y = this.Y; } public void Deconstruct( out Vector2 unitVector, out double length ) { length = this.Length; unitVector = this / length; } } Somewhere else I have: Vector2 foo = ... (Vector2 dir, double len) = foo; This gives me: CS0121: The call is ambiguous between the following methods or

How do I check if a type fits the unmanaged constraint in C#?

我是研究僧i 提交于 2019-12-18 20:10:35
问题 How do I check if a type T fits the unmanaged type constraint, such that it could be used in a context like this: class Foo<T> where T : unmanaged ? My first idea was typeof(T).IsUnmanaged or something similar, but that isn't a property/field of the Type class 回答1: According to unmanaged constraint documentations: An unmanaged type is a type that is not a reference type and doesn't contain reference type fields at any level of nesting. Also it's mentioned in C# language design documentations

C# 7.3 Enum constraint: Why can't I use the enum keyword?

◇◆丶佛笑我妖孽 提交于 2019-12-18 14:05:48
问题 To constrain a generic type parameter to be of an enum type, I previously constrained them like this, which was the best I could go for constraining type T for enums in pre-C# 7.3: void DoSomething<T>() where T : struct, IComparable, IConvertible, IFormattable Now, C# 7.3 adds a new feature to constrain a generic type to System.Enum . I tried using the enum constraint with the VS2017 15.7 update released today, and it compiles successfully when I write it like this (given I have a using

How is it that a struct containing ValueTuple can satisfy unmanaged constraints, but ValueTuple itself cannot?

青春壹個敷衍的年華 提交于 2019-12-06 19:39:12
问题 Consider the following types: (int, int) → managed. struct MyStruct { public (int,int) Value; } → unmanaged! Problem: A non-generic structure MyStruct , which has a managed member (int,int) has been evaluated as managed type. Expected Behavior: A structure which contains a managed member, should be considered as managed, the same way the struct MyStruct { int? Value; } are considered as managed. It seems both types are behaving against the documentations [1] and [2]. Example 1 - unmanaged

How is it that a struct containing ValueTuple can satisfy unmanaged constraints, but ValueTuple itself cannot?

浪子不回头ぞ 提交于 2019-12-04 01:49:57
Consider the following types: (int, int) → managed. struct MyStruct { public (int,int) Value; } → unmanaged! Problem: A non-generic structure MyStruct , which has a managed member (int,int) has been evaluated as managed type. Expected Behavior: A structure which contains a managed member, should be considered as managed, the same way the struct MyStruct { int? Value; } are considered as managed. It seems both types are behaving against the documentations [1] and [2] . Example 1 - unmanaged Constraint class Program { static void DoSomething<T>() where T : unmanaged { } struct MyStruct { public

C# 7.3 Enum constraint: Why can't I use the enum keyword?

三世轮回 提交于 2019-12-03 09:56:28
To constrain a generic type parameter to be of an enum type, I previously constrained them like this, which was the best I could go for constraining type T for enums in pre-C# 7.3: void DoSomething<T>() where T : struct, IComparable, IConvertible, IFormattable Now, C# 7.3 adds a new feature to constrain a generic type to System.Enum . I tried using the enum constraint with the VS2017 15.7 update released today , and it compiles successfully when I write it like this (given I have a using System; directive): void DoSomething<T>() where T : Enum However, using the enum keyword does not work and

How do I check if a type fits the unmanaged constraint in C#?

徘徊边缘 提交于 2019-11-30 19:46:36
How do I check if a type T fits the unmanaged type constraint, such that it could be used in a context like this: class Foo<T> where T : unmanaged ? My first idea was typeof(T).IsUnmanaged or something similar, but that isn't a property/field of the Type class According to unmanaged constraint documentations: An unmanaged type is a type that is not a reference type and doesn't contain reference type fields at any level of nesting. Also it's mentioned in C# language design documentations about unmanaged type constraint : In order to satisfy this constraint a type must be a struct and all the

C# 7.3 Enum constraint: Why can't I use the nullable enum?

寵の児 提交于 2019-11-30 01:19:08
Now that we have enum constraint, why doesn't compiler allow me to write this code? public static TResult? ToEnum<TResult>(this String value, TResult? defaultValue) where TResult : Enum { return String.IsNullOrEmpty(value) ? defaultValue : (TResult?)Enum.Parse(typeof(TResult), value); } The compiler says: Error CS0453 The type 'TResult' must be a non-nullable value type in order to use it as parameter 'T' in the generic type or method 'Nullable' You can, but you have to add another constraint: the struct constraint. public static void DoSomething<T>(T? defaultValue) where T : struct, Enum { }

Equality and polymorphism

半腔热情 提交于 2019-11-29 01:08:59
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 Equals(object obj) { if (object.ReferenceEquals(this, obj)) return true; if (obj is null || obj.GetType()!

C# 7.3 Enum constraint: Why can't I use the nullable enum?

情到浓时终转凉″ 提交于 2019-11-28 22:04:58
问题 Now that we have enum constraint, why doesn't compiler allow me to write this code? public static TResult? ToEnum<TResult>(this String value, TResult? defaultValue) where TResult : Enum { return String.IsNullOrEmpty(value) ? defaultValue : (TResult?)Enum.Parse(typeof(TResult), value); } The compiler says: Error CS0453 The type 'TResult' must be a non-nullable value type in order to use it as parameter 'T' in the generic type or method 'Nullable' 回答1: You can, but you have to add another