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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T07:42:52+00:00 2026-05-14T07:42:52+00:00

C# 4.0 introduced a new type called ‘dynamic’. It all sounds good, but what

  • 0

C# 4.0 introduced a new type called ‘dynamic’. It all sounds good, but what would a programmer use it for?

Is there a situation where it can save the day?

  • 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-14T07:42:52+00:00Added an answer on May 14, 2026 at 7:42 am

    The dynamic keyword is new to C# 4.0, and is used to tell the compiler that a variable’s type can change or that it is not known until runtime. Think of it as being able to interact with an Object without having to cast it.

    dynamic cust = GetCustomer();
    cust.FirstName = "foo"; // works as expected
    cust.Process(); // works as expected
    cust.MissingMethod(); // No method found!
    

    Notice we did not need to cast nor declare cust as type Customer. Because we declared it dynamic, the runtime takes over and then searches and sets the FirstName property for us. Now, of course, when you are using a dynamic variable, you are giving up compiler type checking. This means the call cust.MissingMethod() will compile and not fail until runtime. The result of this operation is a RuntimeBinderException because MissingMethod is not defined on the Customer class.

    The example above shows how dynamic works when calling methods and properties. Another powerful (and potentially dangerous) feature is being able to reuse variables for different types of data. I’m sure the Python, Ruby, and Perl programmers out there can think of a million ways to take advantage of this, but I’ve been using C# so long that it just feels “wrong” to me.

    dynamic foo = 123;
    foo = "bar";
    

    OK, so you most likely will not be writing code like the above very often. There may be times, however, when variable reuse can come in handy or clean up a dirty piece of legacy code. One simple case I run into often is constantly having to cast between decimal and double.

    decimal foo = GetDecimalValue();
    foo = foo / 2.5; // Does not compile
    foo = Math.Sqrt(foo); // Does not compile
    string bar = foo.ToString("c");
    

    The second line does not compile because 2.5 is typed as a double and line 3 does not compile because Math.Sqrt expects a double. Obviously, all you have to do is cast and/or change your variable type, but there may be situations where dynamic makes sense to use.

    dynamic foo = GetDecimalValue(); // still returns a decimal
    foo = foo / 2.5; // The runtime takes care of this for us
    foo = Math.Sqrt(foo); // Again, the DLR works its magic
    string bar = foo.ToString("c");
    

    Read more feature : http://www.codeproject.com/KB/cs/CSharp4Features.aspx

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

Sidebar

Related Questions

A few months ago I was introduced to the new DateTimeOffset type and was
JavaFX is a new technology introduced by Sun. I would like to know what
.NET 4 introduced Code Contracts as a new feature. I'd like to use CC,
I'm fairly new to the world of versioning but would like to introduce Subversion
The iOS 5 SDK introduced a new animation type for table cell insertions and
Apple introduced a new decimal pad keyboard type in iOS 4.1 (I think). I'd
Microsoft introduced a new System.IO.MemoryMappedFiles namespace in .Net 4. Does anyone know if it
Firefox 3 has introduced a new behavior in which line-height, when unset, differs from
I've just read an article that supposedly introduced me to a new concept: Up
In ASP.NET MVC 2, a couple of new action filter attributes were introduced, as

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.