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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T13:13:43+00:00 2026-06-01T13:13:43+00:00

I have started using C# 4.0 and loving the dynamic keyword. However, I am

  • 0

I have started using C# 4.0 and loving the dynamic keyword. However, I am not sure what I am doing can be considered as good practice. Please see the code below:

static void Main()
{
    NoobSauceObject noob = new NoobsauceObject();

    dynamic theReturnType = noob.do(param);

    if (theReturnType.GetType().ToString().Contains("TypeOne"))
        theReturnType.ExecuteMethodOfTypeOne();
    else if (theReturnType.GetType().ToString().Contains("TypeTwo"))
        theReturnType.ExecuteMethodOfTypeTwo();
    else
        throw new ArgumentException("");
}

Is there a better way of doing this? I found the above method quite easy and have been using it but not sure if it is something I should stick with in the long run.

EDIT: If I were to do the same using .NET 3.5 or lower, or without the dynamic keyword, what would be a good implementation?

Thanks in advance!! 🙂

  • 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-01T13:13:45+00:00Added an answer on June 1, 2026 at 1:13 pm

    It looks to me like you’re just doing a type-test between two unrelated types. If possible, I’d look at polymorphism here, or at least: implementing a common interface. However, the following would be fine too:

    var typeOne = theReturnType as TypeOne;
    if(typeOne != null) typeOne.ExecuteMethodOfTypeOne();
    else {
        var typeTwo = theReturnType as TypeTwo;
        if(typeTwo != null) typeTwo.ExecuteMethodOfTypeTwo();
        else throw new ArgumentException("somethingMeaningful");
    }
    

    However, my preferred option is:

    var typed = theReturnType as ISomeInterface;
    if(typed != null) typed.SomeMethod();
    else throw new ArgumentException("somethingMeaningful");
    

    where TypeOne and TypeTwo might use explicit interface implementation to expose the method on their API:

    public class TypeOne : ISomeInterface {
        void ISomeInterface.SomeMethod() { ExecuteMethodOfTypeOne(); }
        public void ExecuteMethodOfTypeOne() {
            // ...
        }
    }
    

    (and likewise TypeTwo)

    I can see no real use for dynamic here; For the return type of noob.do(param), object would be fine in the first example – or ISomeInterface would be even better.

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

Sidebar

Related Questions

I have started using Subsonic for a side project and have been loving using
We have just started using requirejs in our project and we are loving it.
I have started using devise for my rails app, however i need to make
Recently I have started using Lucene. However, after few days I've spotted that queries
I have started using Jython as it seems to be a excellent language, and
I have started using Linq to SQL in a (bit DDD like) system which
I have started using aspects for parameter validation in our development framework. It works
We have started using Spring framework in my project. After becoming acquainted with the
I have started using Linq to SQL for a project im working on and
I have started using of generics in Delphi 2010 but I have a problem

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.