Skip to content
Discussion options

You must be logged in to vote

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 (int to long) in reverse during an explicit conversion, while not applying standard explicit conversions such as ulong to int. I'm curious if anyone has an intuition for what the spec is doing here.

Walkthrough

This 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.
}

(UnixTime)i is a user-defined …

Replies: 5 comments 10 replies

Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
2 replies
@jilles-sg
Comment options

@RioMcBoo
Comment options

Comment options

You must be logged in to vote
7 replies
@RioMcBoo
Comment options

@RioMcBoo
Comment options

@jnm2
Comment options

jnm2 Nov 30, 2025
Collaborator

@RioMcBoo
Comment options

@jnm2
Comment options

jnm2 Nov 30, 2025
Collaborator

This comment was marked as off-topic.

@333fred

This comment was marked as off-topic.

Comment options

You must be logged in to vote
0 replies
Answer selected by jnm2
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
6 participants
Converted from issue

This discussion was converted from issue #8185 on June 11, 2024 15:59.