-
|
Version Used: C# 12, though it's present in all of them Steps to Reproduce: Diagnostic Id: None Expected Behavior: As per the built-in numeric conversion rules, the code above should fail. Actual Behavior: Instead it inserts an implicit conversion from |
Beta Was this translation helpful? Give feedback.
Replies: 5 comments 10 replies
-
|
As far as I understand this is not a bug but behavior defined in the spec (10.5.5 User-defined explicit conversions) which allows using intermediate standard explicit conversions in this case. |
Beta Was this translation helpful? Give feedback.
-
Beta Was this translation helpful? Give feedback.
-
Beta Was this translation helpful? Give feedback.
This comment was marked as off-topic.
This comment was marked as off-topic.
-
|
This is not a compiler bug, but is how explicit conversions have been specified since C# version 1. I'm a little confused about what the intuition is for the spec to apply a standard implicit conversion ( WalkthroughThis is the reported sample: public struct UnixTime
{
public static explicit operator UnixTime(int i) => default;
}
public class C
{
public UnixTime M(long i) => (UnixTime)i; // This cast should fail to compile, but it compiles successfully.
}
These special cases don't apply;
The set of types
According to this step, the compiler must include in "Encompassing" is defined in §10.5.3 Evaluation of user-defined conversions:
Because there is a standard implicit conversion from the type (This is the point at which the algorithm would diverge for |
Beta Was this translation helpful? Give feedback.


This is not a compiler bug, but is how explicit conversions have been specified since C# version 1.
I'm a little confused about what the intuition is for the spec to apply a standard implicit conversion (
inttolong) in reverse during an explicit conversion, while not applying standard explicit conversions such asulongtoint. I'm curious if anyone has an intuition for what the spec is doing here.Walkthrough
This is the reported sample:
(UnixTime)iis a user-defined …