Click here to Skip to main content
Click here to Skip to main content

Tagged as

dynamic keyword - Represents an object whose operations will be resolved at runtime

, 15 May 2011 CPOL
Rate this:
Please Sign up or sign in to vote.
dynamic keyword - Represents an object whose operations will be resolved at runtime
C#4.0 introduces a new type, dynamic.It is treated as System.Object, but in addition, any member access (method call, field, property, or indexer access, or a delegate invocation) or application of an operator on a value of such type is permitted without any type checking, and its resolution is postponed until run-time.
This is the one of the best example uses of dynamic:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
    public class Colour
    public class Red : Colour
    public class Green : Colour
    class Program
        static void Main(string[] args)
            Colour color = null;
            color = new Red();
            color = new Green();
        static void GetColour(Colour color)
            /* avoiding this lines
            if (color is Red)
            if (color is Green)
             * */
            //type checking has to be done at runtime.
            dynamic dynColour = color;
        static void Fill(Red red)
        static void Fill(Green green)
The role of the C# compiler here is simply to package up the necessary information about “what is being done to dynColour” so that the runtime can pick it up and determine what the exact meaning of it is given an actual object dynColour. Think of it as deferring part of the compiler’s job to runtime.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Software Developer
India India

Hi I' Ambarish from India. I've three and half year of experience in application
development solutions.

Educational Qualification

MCA -  from KristuJyoti College of management and technology (MG

B.Sc (Physics) - from NSS College, Changanacherry (MG University)

Skill Set

C#,winforms,,MVC 3, Java,Design Patterns,VB, Android,JQuery Mobile,SQL,MySqlUML.

Comments and Discussions

GeneralNice Example PinmemberWayne Gaylard12-May-11 4:35 
GeneralRe: Nice Example Pinmemberambarishtv12-May-11 4:55 
GeneralNot Visual PinmvpRichard MacCutchan12-May-11 0:18 
While this can be seen in Visual Studio 2010, it is actually a part of the C# specification for version 4. VS 2010 is merely an IDE wrapper around the compiler.
The best things in life are not things.

GeneralRe: Not Visual Pinmemberambarishtv12-May-11 1:27 
GeneralRe: Not Visual PinmvpRichard MacCutchan12-May-11 4:02 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.150327.1 | Last Updated 15 May 2011
Article Copyright 2011 by ambarishtv
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid