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

  • SEARCH
  • Home
  • 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 6773273
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T15:38:55+00:00 2026-05-26T15:38:55+00:00

Consider the following code: class Program { public static explicit operator long(Program x) {

  • 0

Consider the following code:

class Program
{
    public static explicit operator long(Program x) { return 47; }

    static int Main(string[] args)
    {
        var x = new Program();
        Console.WriteLine((decimal) x);
    }
}

To my surprise, this outputs 47; in other words, the explicit operator long is called even though the cast is to decimal.

Is there something in the C# spec that explicitly says that this should happen (if so, where exactly) or is this the result of some other rule(s) I’m missing?

  • 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-05-26T15:38:55+00:00Added an answer on May 26, 2026 at 3:38 pm

    I’ve found the answer. First of all, there is the concept of one type being encompassed by another, which is defined in 6.4.3 Evaluation of user-defined conversions as follows:

    If a standard implicit conversion (§6.3.1) exists from a type A to a
    type B, and if neither A nor B are interface-types, then A is said to
    be encompassed by B, and B is said to encompass A.

    6.3.1 Standard implicit conversions states that “Implicit numeric conversions (§6.1.2)” are a standard implicit conversion, and 6.1.2 Implicit numeric conversions in turn defines an implicit conversion from long to decimal. Therefore, long is encompassed by decimal.

    Next, 6.4.5 User-defined explicit conversions states that one of the stages in determining whether an explicit conversion is applicable is to:

    Find the set of applicable user-defined and lifted conversion
    operators, U. This set consists of the user-defined and lifted
    implicit or explicit conversion operators declared by the classes or
    structs in D that convert from a type encompassing or encompassed by S
    to a type encompassing or encompassed by T. If U is empty, the
    conversion is undefined and a compile-time error occurs.

    Here, D refers to the result of an earlier step which, in this case, contains only decimal, Program and object. Thus, the set U will contain the Program-to-long explicit operator I declared because long is encompassed by decimal (as we found earlier).

    One of the next steps selects long as the most specific target type, TX.

    Finally, the last step in that same algorithm states:

    Finally, apply the conversion:

    • If S is not SX, then a standard explicit conversion from S to SX is performed.
    • The most specific user-defined conversion operator is invoked to convert from SX to TX.
    • If TX is not T, then a standard explicit conversion from TX to T is performed.

    Here, S and SX are both Program, so the first part does nothing. TX was selected to be long and T is the target type, decimal, so the last part executes the standard conversion from long to decimal.

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

Sidebar

Related Questions

Consider the following code: class Program { static void Main(string[] args) { Department deathStar
Consider the following code: class Program { static void Main(string[] args) { A a
Consider the following code: class Program { static void Main(string[] args) { new Program().Run(args);
Consider the following code: namespace ConsoleApplication1 { class Program { static void Main(string[] args)
Consider the following code:- class Name { {System.out.println(hi);} public static void main(String[] args) {
Consider the following code: class Program { void Foo<T>() { } static void Main(string[]
Consider the following C# code: class Program { static public void Print(string toPrint) {
Consider the following code: public class MyClass { public static string MyStaticMethod() { //string
Consider following code public class City { public string Name { get { return
Consider the following code: using System; namespace ConsoleApplication2 { class Program { static void

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.