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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T21:51:17+00:00 2026-06-18T21:51:17+00:00

Given the following code: using System; namespace Test721 { class MainClass { public static

  • 0

Given the following code:

using System;

namespace Test721
{
    class MainClass
    {
        public static void Main(string[] args)
        {
            Console.WriteLine(new A()); //prints 666
            Console.WriteLine(new B()); //prints 666
            Console.ReadLine();
        }
    }

    public class A
    {
        public static implicit operator int(A a)
        {
            return 666;
        }
    }

    public class B : A
    {
        public static implicit operator double(B b)
        {
            return 667;
        }
    }
}

the results are as in the comments – both lines print 666.

I’d expect Console.WriteLine(new B()); to write 667, while there is a double overload of Console.WriteLine.

Why is that happening?

  • 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-18T21:51:18+00:00Added an answer on June 18, 2026 at 9:51 pm

    This is covered in section 7.4.3.4 of the 3.5 C# language spec. This section deals in overload resolution and how conversions are considered in this process. The applicable line is

    If an implicit conversion from T1 to T2 exists, and no implicit conversion from T2 to T1 exists, C1 is the better conversion.

    In this case there is an implicit conversion from B to both int (T1) and double (T2). There is an implicit conversion from int to double but not the other way around. Hence the implicit conversion from B to int is considered better and wins.

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

Sidebar

Related Questions

Given the following code: using System; namespace Sandbox { class CommandLine { static void
Take this code: using System; namespace OddThrow { class Program { static void Main(string[]
Consider the following C# code: using System.Xml.Linq; namespace TestXmlParse { class Program { static
Given the following code snippet: using System; using Foo = System.Int32; namespace ConsoleApplication3 {
Consider the following code: using System; using System.Runtime.InteropServices; namespace Demo { class Program {
The following C++ code gives an error while compiling: #include<iostream> using namespace std; class
Given the following code, I try to implement the public static Point operator +(int
I have the following minimal code: using namespace System; long get_prop( Object^ v )
I have a REST API using Django Tastypie. Given the following code The models
Given the following lines of code which is using JQTOUCH: $('#customers').bind('pageAnimationEnd', function(e, info){ if

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.