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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T01:22:23+00:00 2026-05-14T01:22:23+00:00

I like the new Dynamic keyword and read that it can be used as

  • 0

I like the new Dynamic keyword and read that it can be used as a replacement visitor pattern.

It makes the code more declarative which I prefer.

Is it a good idea though to replace all instances of switch on ‘Type’ with a class that implements dynamic dispatch.

class VistorTest
{
    public string DynamicVisit(dynamic obj)
    {
        return Visit(obj);
    }


    private string Visit(string str)
    {
        return "a string was called with value " + str;
    }


    private string Visit(int value)
    {
        return "an int was called with value " + value;
    }
}
  • 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-14T01:22:23+00:00Added an answer on May 14, 2026 at 1:22 am

    It really depends on what you consider a “good idea”.

    This works, and it works in a fairly elegant manner. It has some advantages and some disadvantages to other approaches.

    On the advantage side:

    1. It’s concise, and easy to extend
    2. The code is fairly simple

    For the disadvantages:

    1. Error checking is potentially more difficult than a classic visitor implementation, since all error checking must be done at runtime. For example, if you pass visitorTest.DynamicVisit(4.2);, you’ll get an exception at runtime, but no compile time complaints.
    2. The code may be less obvious, and have a higher maintenance cost.

    Personally, I think this is a reasonable approach. The visitor pattern, in a classic implementation, has a fairly high maintenance cost and is often difficult to test cleanly. This potentially makes the cost slightly higher, but makes the implementation much simpler.

    With good error checking, I don’t have a problem with using dynamic as an approach here. Personally, I’d probably use an approach like this, since the alternatives that perform in a reasonable manner get pretty nasty otherwise.

    However, there are a couple of changes I would make here. First, as I mentioned, you really need to include error checking.

    Second, I would actually make DynamicVisit take a dynamic directly, which might make it (slightly) more obvious what’s happening:

    class VistorTest
    {
        public string DynamicVisit(dynamic obj)
        {
            try
            {
                return Visit(obj);
            }
            catch (RuntimeBinderException e)
            {
                // Handle the exception here!
                Console.WriteLine("Invalid type specified");
            }
            return string.Empty;
        }
    
         // ...Rest of code
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Not long time before I've discovered, that new dynamic keyword doesn't work well with
suppose I declare a dynamic array like int *dynArray = new int [1]; which
With the new dynamic capabilities in .NET 4.0, it seems like it should be
Recently I have been exploring using the fancy new dynamic keyword introduced in C#
I have used the KEYWORD dynamic and it is only available in .net 4.0
I am planning to use dynamic keyword for my new project. But before stepping
I have a a dynamic value (implementation of IDynamicMetaObjectProvider) that I'd like to call
Does Scala support something like dynamic properties? Example: val dog = new Dynamic //
Is it possible to create an AOP like interceptor using Scalas new Dynamic Type
I would like to create a new dynamic chart control. This chart will have

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.