Free Trial

Safari Books Online is a digital library providing on-demand subscription access to thousands of learning resources.


  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • DownloadDownload
  • PrintPrint
Share this Page URL
Help

Dynamics

The C# dynamic type allows the developer to bypass compile-time type checking, instead passing the responsibility of type resolution to the Dynamic Language Runtime (DLR). Dynamics are used extensively within ASP.NET Web Pages, and are at the heart of the standard data access strategy.

A dynamic variable can essentially store any value and will not be checked by the compiler during the build process; the DLR will deal with the resolution of types at runtime. The following code may look a bit strange to anyone used to programming in a strongly-typed, type-safe environment such as C#, but it is valid code and will compile and run successfully:

dynamic myDynamic = "hello";
myDynamic = 2.94e-56d;
myDynamic = DateTime.Today.Year;

This ability to store any type makes them really convenient for use in collections where you don't necessarily want to store the same data type in each index. The following code will demonstrate this further:


  

You are currently reading a PREVIEW of this book.

                                                                                        

Get instant access to over
$1 million worth of books and videos.

  

Start a Free Trial