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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T06:49:54+00:00 2026-05-16T06:49:54+00:00

Introduction I am aware that "user-defined conversions to or from a base class are

  • 0

Introduction

I am aware that "user-defined conversions to or from a base class are not allowed". MSDN gives, as an explanation to this rule, "You do not need this operator."

I do understand that a user-defined conversion to a base class is not needed, as this obviously done implicitly. However, I do need a conversion from a base class.

In my current design, a wrapper of unmanaged code, I use a pointer, stored in an Entity class.
All the classes using a pointer derive from that Entity class, for example, a Body class.

I therefore have:

Method A

class Entity
{
    IntPtr Pointer;

    Entity(IntPtr pointer)
    {
        this.Pointer = pointer;
    }
}

class Body : Entity
{
    Body(IntPtr pointer) : base(pointer) { }

    explicit operator Body(Entity e)
    {
        return new Body(e.Pointer);
    }
}

This cast is the illegal one. (Note that I didn’t bother writing the accessors).
Without it, the compiler will allow me to do:

Method B

(Body)myEntity
...

However, at runtime, I will get an exception saying this cast is impossible.

Conclusion

Therefore here I am, needing an user-defined conversion from a base class, and C# refuses it to me. Using method A, the compiler will complain but the code would logically work at runtime. Using method B, the compiler will not complain but the code will obviously fail at runtime.

What I find strange in this situation is that MSDN tells me I do not need this operator, and the compiler acts as if it was possible implicitly (method B). What am I supposed to do?

I am aware that I can use:

Solution A

class Body : Entity
{
    Body(IntPtr pointer) : base(pointer) { }

    static Body FromEntity(Entity e)
    {
        return new Body(e.Pointer);
    }
}

Solution B

class Body : Entity
{
    Body(IntPtr pointer) : base(pointer) { }

    Body(Entity e) : base(e.Pointer) { }
}

Solution C

class Entity
{
    IntPtr Pointer;

    Entity(IntPtr pointer)
    {
        this.Pointer = pointer;
    }

    Body ToBody()
    {
        return new Body(this.Pointer);
    }
}

But honestly, all the syntaxes for these are horrible and should in fact be casts.
So, any way to make the casts work? Is it a C# design flaw or did I miss a possibility? It’s as if C# didn’t trust me enough to write my own base-to-child conversion using their cast system.

  • 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-16T06:49:55+00:00Added an answer on May 16, 2026 at 6:49 am

    It’s not a design flaw. Here’s why:

    Entity entity = new Body();
    Body body = (Body) entity;
    

    If you were allowed to write your own user-defined conversion here, there would be two valid conversions: an attempt to just do a normal cast (which is a reference conversion, preserving identity) and your user-defined conversion.

    Which should be used? Would you really want is so that these would do different things?

    // Reference conversion: preserves identity
    Object entity = new Body();
    Body body = (Body) entity;
    
    // User-defined conversion: creates new instance
    Entity entity = new Body();
    Body body = (Body) entity;
    

    Yuk! That way madness lies, IMO. Don’t forget that the compiler decides this at compile-time, based only on the compile-time types of the expressions involved.

    Personally I’d go with solution C – and possibly even make it a virtual method. That way Body could override it to just return this, if you want it to be identity preserving where possible but creating a new object where necessary.

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

Sidebar

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.