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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T22:24:38+00:00 2026-05-19T22:24:38+00:00

class Base { } class Derived1 : Base { } class Derived2 : Base

  • 0
class Base
{
}

class Derived1 : Base
{
}

class Derived2 : Base
{
    public static explicit operator Derived1(Derived2 d2)
    {
        return new Derived1();
    }
}

class Test
{
    static void Main()
    {
        Base bd2 = new Derived2();

        Derived1 d2ConvertedD1 = (Derived1)bd2; //throws InvalidCastException

    }
}

Unable to cast object of type 'ConsoleApplication1.Derived2' to type 'ConsoleApplication1.Derived1'.

Why? What is wrong with my operator conversion?

  • 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-19T22:24:39+00:00Added an answer on May 19, 2026 at 10:24 pm

    Look at the signature of your operator:

    public static explicit operator Derived1(Derived2 d2);
    

    Notice it’s static. What you’re seeing is similar to the limitation of method overload resolution.

    It’s essentially the same reason the below outputs “Object” instead of “String”:

    static void WriteObject(object obj) { Console.WriteLine("Object"); }
    static void WriteObject(string str) { Console.WriteLine("String"); }
    
    object obj = "I am a string.";
    WriteObject(obj);
    

    That is, the compiler needs to pick an overload at compile-time. In the case of casting from a Base to a Derived1, there is no overload with the proper signature, so it attempts an actual downcast. Declaring bd2 as Derived2, as others have mentioned, would “fix” this by enabling the compiler to select your custom conversion.

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

Sidebar

Related Questions

Consider the following code: public abstract class Base { public void getAnswer(); } public
Consider: template <typename T> class Base { public: static const bool ZEROFILL = true;
using System; namespace random { interface IHelper { void HelpMeNow(); } public class Base
Let's say we have these two classes: public class Base { public static int
#include <iostream> template <class Derived> class Base { public: void method1() { static_cast<Derived*>(this)->method1(); }
class Base { int i = 99; public void amethod() { System.out.println(Base.amethod()); } Base()
#include <iostream> class base { public: virtual void print (int a) { std::cout <<
Consider these classes. class Base { ... }; class Derived : public Base {
My style of coding includes the following idiom: class Derived : public Base {
I have an abstract base class and derived class: type TInterfaceMethod = class public

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.