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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T10:30:18+00:00 2026-06-13T10:30:18+00:00

In the following code, i have an overloaded method, one that takes a parameter

  • 0

In the following code, i have an overloaded method, one that takes a parameter of type ClazzA and the other of type ClazzB. In the code shown, the first GetDescription method (the one that takes ClazzA as a parameter) is called. I think i understand why.
My question is..is there an elegant way of having the method that takes clazzB called first if the underlying object is of type classB (without having to inspect each object and casting it to clazzB)?

public class ClazzA
{
    public virtual string Descr { get { return "A"; } }
}

public class ClazzB : ClazzA
{
    public override string Descr { get { return "B"; } }
}

public static class test
{
    public static void Main()
    {
        ClazzA test = new ClazzB();
        GetDecription(test);
    }

    public static void GetDecription(ClazzA someClazz)
    {
        Debug.WriteLine("I am here");
    }

    public static void GetDecription(ClazzB someClazz)
    {
        Debug.WriteLine("I want to be here");
    }
}

Output: “I am here”

I really want the 2nd method to be called since ‘test’ is of type ClassB. Curerently the only two solutions i have is:

  1. if (test is ClazzB)
    return GetDescription( (ClazzB) test );

or

  1. In ClassA do pretty much the same thing…check the type and delegate to the 2nd method

Both of these require inspection of the object to determine its type

  • 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-13T10:30:19+00:00Added an answer on June 13, 2026 at 10:30 am

    Overloads are determined at compile time. The compile time type of the reference is ClazzA so that overload is chosen. What you are asking for is related to multiple dispatch. C# and many other languages like C++ and Java only support single dispatch (via virtual methods). There are a number of ways people have come up with to work around this. The purest OO way of doing this is the visitor pattern. You modify the classes to contain a method (Accept) which then passes the this reference to a method on the visitor (Visit). This works because you override the Accept method in each subclass so that this will be the object’s actual type. All the visitor needs is a specific method for each subclass that you want to support (see wikipedia for more details).

    A sample:

    public class ClazzA
    {
       public virtual string Accept(ClassVisitor visitor)
       {
          return visitor.Visit(this);
       }
    }
    
    public class ClazzB : ClazzA
    {
       public override string Accept(ClassVisitor visitor)
       {
          return visitor.Visit(this);
       }
    }
    
    public abstract class ClassVisitor
    {
      public abstract string Visit(ClazzA a);
      public abstract string Visit(ClazzB b);
    }
    
    public class GetDescriptionVisitor : ClassVisitor
    {
        public override string Visit(ClazzA a)
        {
           return "A";
        }
    
        public override string Visit(ClazzB b)
        {
           return "B";
        }
    }
    

    Usage:

    ClassVisitor visitor = new GetDescriptionVisitor();
    ClazzA b = new ClazzB();
    Console.WriteLine(b.Accept(visitor)); // prints "B"
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have following code <div id=main> <div id=one> </div> <div id=two> </div> <div id=three>
I have following code for loading image from url in xml parsing endElement method
I have the following Groovy code snippet that is attempting to use operator overloading
I have some IronPython code that is creating a Mutex using the following constructor:
I have the following code that splits a string on newlines and converts it
This is for Delphi Prism. Say, I have the following enum SET type that
I want to overload a cast operator, I have the following piece of code
Suppose I have following code package memoryleak; public class MemoryLeak { public static int
Hi I have following code block public class Driver { static String x =
I have following code in initialization im = imread('Image02.tif'); figure(); imagesc(im); colormap(gray); [hImage hfig

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.