You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Version Used:
.NET Sdk v2.1.400 (as shown by dotnet --version)
IDE: Visual Studio Community 2017 v15.8.1
OS: Windows 8.1
Steps to Reproduce:
Create a custom struct type (example in C#) -- for instance public struct MyStruct { }
Overload cast operator from Nullable<MyStruct> to MyStruct:
public static implicit operator MyStruct(Nullable<MyStruct> nv) => nv.GetValueOrDefault();
Compile the project.
Expected Behavior:
The project compiles successfully and user code should be able to perform implicit casts from MyStruct? values to MyStruct ones. For example, consider the following code:
public class Foo
{
public void Method(MyStruct myStruct) { }
}
Nullable<MyStruct> nullMyStruct = null;
Nullable<MyStruct> notNullMyStruct = new MyStruct();
var foo = new Foo();
In the below snippet, due to the implicit cast, foo.Method should receive an equivalent of new MyStruct()
foo.Method(nullMyStruct);
And similarly, here the implicit cast should pass to foo.Method what is stored in notNullMyStruct.Value
foo.Method(notNullMyStruct);
Actual Behavior:
A compilation error occurs:
error CS0555: User-defined operator cannot take an object of the enclosing type and convert to an object of the enclosing type
The compiler performs some type of erasure preventing it from distinguishing the Nullable<MyStruct> type from the MyStruct one.
Personal note
The particular use case for such a cast operator is illustrated in a question I asked in Stackoverflow a couple of days ago. From there I was advised to raise the current issue.
I understand the importance of the Nullable<T> type not permitting implicit casts to T which is part of the design of this type. However, the documentation doesn't seem to explicitly specify if the user code should be able to overcome this limitation, by defining a custom cast operator, or not (due to violating the design). The compiler error most certainly does not help finding out.
The text was updated successfully, but these errors were encountered:
This behavior is "By Design" for C#. There is no way to customize the process of converting between a struct and it's nullable wrapper. If you want to suggest a behavior change please open this on the charplang repository.
Version Used:
.NET Sdk v2.1.400 (as shown by
dotnet --version
)IDE: Visual Studio Community 2017 v15.8.1
OS: Windows 8.1
Steps to Reproduce:
Create a custom struct type (example in C#) -- for instance
public struct MyStruct { }
Overload cast operator from
Nullable<MyStruct>
toMyStruct
:public static implicit operator MyStruct(Nullable<MyStruct> nv) => nv.GetValueOrDefault();
Compile the project.
Expected Behavior:
The project compiles successfully and user code should be able to perform implicit casts from
MyStruct?
values toMyStruct
ones. For example, consider the following code:In the below snippet, due to the implicit cast,
foo.Method
should receive an equivalent ofnew MyStruct()
And similarly, here the implicit cast should pass to
foo.Method
what is stored innotNullMyStruct.Value
Actual Behavior:
A compilation error occurs:
The compiler performs some type of erasure preventing it from distinguishing the
Nullable<MyStruct>
type from theMyStruct
one.Personal note
The particular use case for such a cast operator is illustrated in a question I asked in Stackoverflow a couple of days ago. From there I was advised to raise the current issue.
I understand the importance of the
Nullable<T>
type not permitting implicit casts toT
which is part of the design of this type. However, the documentation doesn't seem to explicitly specify if the user code should be able to overcome this limitation, by defining a custom cast operator, or not (due to violating the design). The compiler error most certainly does not help finding out.The text was updated successfully, but these errors were encountered: