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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T00:39:19+00:00 2026-05-14T00:39:19+00:00

-Edit- Alternative question/example How do i cast A to object to class A when

  • 0

-Edit- Alternative question/example How do i cast A to object to class A when B can typcast to A?


I have class A, B, C. They all can implicitly convert to a string

public static implicit operator A(string sz_) {  ... return sz; }

I have code that does this

object AClassWhichImplicitlyConvertsToString

{
    ...
    ((IKnownType)(String)AClassWhichImplicitlyConvertsToString).KnownFunc()
}

The problem is, AClassWhichImplicitlyConvertsToString isnt a string even though it can be typecast into one implicitly. I get a bad cast exception. How do i say its ok as long as the class has an operator to convert into a string?

  • 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-14T00:39:19+00:00Added an answer on May 14, 2026 at 12:39 am

    There is almost certainly a better way of doing whatever it is you’re trying to do. If you provide more context, you’ll get more helpful answers.

    If instead of (or as well as) making your classes implicitly covert to string you also give them a ToString override, you can then say:

    ((KnownType)AClassBlah.ToString()).KnownFunc()
    

    However, you’ll then get an exception on trying to cast a string into KnownType. So I have to ask: why are you trying to go via string in this situation? Casts are generally an ugly-ass thing that make you think “Maybe my design needs refactoring one day”. They’re not something you design into your class library as a recommended usage pattern. They’re a low-level facility with predictable behaviour, so there is no way (and no good reason to provide a way) to override what an explicit cast does.

    Update

    Judging from your comment you are mixing together runtime polymorphism and static (compile time) conversion. They don’t mix too well. Are you previously a user of dynamically typed languages? It seems like you might be. If you have a method:

    void FiddleWithObject(object obj)
    {
        // whatever
    }
    

    Then the author of that method has no compile-time knowledge of what operations are available on obj. So they can say:

    void FiddleWithObject(object obj)
    {
        if (obj is IFiddly)
        {
            // Cool
            obj.Fiddle();
        }
        else
            throw new Exception("Wrong type of object");
    }
    

    This then blows up at compile time for classes that aren’t IFiddly. But in a statically typed language, you can say:

    void FiddleWithObject(IFiddly obj)
    {
        obj.Fiddle(); 
    }
    

    This will blow up at compile time if the wrong kind of object is passed, and you don’t need to check anything at runtime. Less code, bugs found sooner… how neat is that?

    The implicit conversion feature is part of the operator overloading set of features. These are all very much tied to static types. They are resolved at compile time based on the known type of the object. So if you don’t know the actual class of an object, there is no (built-in) way to call operators on it. It just doesn’t mix with dynamic typing.

    If it is possible to get a string (such as a “name”) from an IFiddly object, then you can make it a property on that interface:

    public interface IFiddly
    {
        void Fiddle();
        string Name { get; }
    }
    

    Or (as I noted before) you could just override ToString on any object, as that’s a virtual method on the object class that all classes ultimately inherit from. So by saying:

    var str = someObject.ToString();
    

    You are going to be calling the ToString implementation defined in whatever class someObject is an instance of.

    In summary:

    • virtual and abstract methods, and supported interfaces: these are for dynamic, runtime typing and polymorphism.
    • operator and implicit conversion overloading (and generics): these are for compile-time, static typing.
    • casts are disgusting.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 367k
  • Answers 367k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer An icon file contains one or more images of different… May 14, 2026 at 4:47 pm
  • Editorial Team
    Editorial Team added an answer For your first question: X row(s) fetched in Y s… May 14, 2026 at 4:47 pm
  • Editorial Team
    Editorial Team added an answer You can write SQL statements directly or use DTS if… May 14, 2026 at 4:47 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.