Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8639117
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T10:55:08+00:00 2026-06-12T10:55:08+00:00

When I try to use a user-defined cast operator from an interface type to

  • 0

When I try to use a user-defined cast operator from an interface type to a generic struct type, I get a compile error stating the type can’t be converted:

public interface J { }
public struct S<T> {
    public static explicit operator S<T>(T value) {
        return new S<T>();
    }
}
public static class C {
    public static S<J> Test(J j) {
        return (S<J>)j; // <- error: cannot convert type 'J' to type 'S<J>'
    }
}

Note that if J were a class, the conversion would work.

There is a similar question about converting to a class, with the answer being that a derived class might implement the interface and create ambiguity over whether the user-defined cast should be used. But for structs there can be no derived type, and the compiler knows that my struct does not implement J.

Perhaps it’s to avoid unexpected semantic changes as interfaces implementations new interfaces? Maybe it’s just accidental? Maybe I’m making a naive mistake? A quote from the spec would be great, but really I’d like the reason it was designed that way in the first place.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T10:55:09+00:00Added an answer on June 12, 2026 at 10:55 am

    Well, the system can’t do an actual cast because S<J> does not implement J. It works fine if you change your declaration:

    public struct S<T> : J {...}
    

    In the case of classes, it’s possible for a sub-type of a class to be declared which does implement J. But structs cannot be extended, so if S doesn’t implement J then you can guarantee that no class that “is a” S will ever implement J either. Therefore no J will ever be an S<T>.

    So why doesn’t it invoke your explicit cast operator? I think the answer is in the C# spec, as described in this SO post.


    6.4.1 Permitted user-defined conversions

    C# permits only certain user-defined conversions to be declared. In particular, it is not possible to redefine an already existing implicit or explicit conversion.
    For a given source type S and target type T, if S or T are nullable types, let S0 and T0 refer to their underlying types, otherwise S0 and T0 are equal to S and T respectively. A class or struct is permitted to declare a conversion from a source type S to a target type T only if all of the following are true:

    • S0 and T0 are different types.
    • Either S0 or T0 is the class or struct type in which the operator declaration takes place.
    • Neither S0 nor T0 is an interface-type.
    • Excluding user-defined conversions, a conversion does not exist from S to T or from T to S.

    So if I’m reading that right, because J is an interface, it is not recognized as a candidate for your explicit cast operator.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

From the SQL::Statement::Functions documentation: Creating User-Defined Functions ... More complex functions can make use
I have scenario to use the Temporary user defined type variable in transaction, but
I try to use a variable in my kshell script but can't get a
i try to use TBXML and getting this error TBXML class method +tbXMLWithURL not
I have a type defined like this: struct DynTab_s { int object_count; //other fields
Using SQL 2008 So I created a User Defined type: CREATE TYPE dbo.ServiceType AS
I try to make a clean trigger with user-defined variables, so this is my
We use an assembly with some user defined functions in our installation of SQL
I'm looking at an example from apple how to use vertex arrays: typedef struct
I have been playing around with using error handling. In particular with user defined

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.