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 6165787
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T22:13:37+00:00 2026-05-23T22:13:37+00:00

Code: void Main() { C.F(); } public class C { public static void F()

  • 0

Code:

void Main()
{
    C.F();
}
public class C
{
    public static void F()
    {
        var a = new A { i = 1, d = 2.5m };
        var b = new B(a);
        I(b);
        D(b);
    }
    static void I(int i) { Console.WriteLine("int is: " + i); }  
    static void D(decimal d) { Console.WriteLine("decimal is: " + d); }
}
public class A
{
    public int i;
    public decimal d;
}
public class B
{
    A _a;
    public B(A a) { _a = a; }
    public static implicit operator int(B b) { return b._a.i; }
    public static implicit operator decimal(B b) { return b._a.d; }
}

OUTPUT:
int is: 1
decimal is: 2.5

Comment out:

//public static implicit operator decimal(B b) { return b._a.d; }

OUTPUT:
int is: 1
decimal is: 1

What is going on when the second version runs and outputs 1 for both cases?

  • 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-23T22:13:38+00:00Added an answer on May 23, 2026 at 10:13 pm

    My guess is that the compiler sees that there is an implicit conversion from B to int, and an implicit (built-in) conversion from int to decimal, so that it can use both in sequence. In other words, the call becomes D((decimal)(int)b).

    Note that nothing is being truncated; rather, an int is being promoted to a decimal. If instead you comment out the int conversion, I expect that I(b) will fail, since even though there is an implicit cast from B to decimal, there is no implicit cast from decimal to int.

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

Sidebar

Related Questions

The following code public class GenericsTest2 { public static void main(String[] args) throws Exception
I have the following code: public class Test { public static void Main() {
The following code does not compile: public class GenericsTest { public static void main(String[]
copy paste the following code in new C# console app. class Program { static
This code compiles: private static void Main(string[] args) { bool? fred = true; if
In my computer this code takes 17 seconds (1000 millions times): static void Main(string[]
Given this piece of code: (void)someFunction(void) { int array[] = {1,2,3,4,5,6,7,8,9,10}; } Where are
When compiling the following code: void DoSomething(int Numbers[]) { int SomeArray[] = Numbers; }
Consider the following line of code: private void DoThis() { int i = 5;
I cannot prepare a query that I need. I have code: public class Dog

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.