Is it a bad idea to use the new Dynamic Keyword as a replacement switch statement?

Posted by WeNeedAnswers on Stack Overflow See other posts from Stack Overflow or by WeNeedAnswers
Published on 2010-03-25T17:01:50Z Indexed on 2010/03/25 17:03 UTC
Read the original article Hit count: 242

Filed under:

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(object obj)
    {
        return Visit((dynamic)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;
    }
}

© Stack Overflow or respective owner

Related posts about c#4.0