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

Tagged as

Dynamic Keyword In C# 4.0 - Part 3

, 19 Apr 2011
Rate this:
Please Sign up or sign in to vote.
Difference between Dynamic and Object type

Introduction

In C#, all types, predefined, user-defined, reference types and value types, inherit directly or indirectly from Object. We can assign values of any type to variables of typeobject. When a value of any type converted to object is called Boxing and vice versa (Unboxing), this is not the case in dynamic (it stores metadata about the dynamic calls at compile time and applies it to calls at runtime). Let’s take a function that returns System.Object, an instance that can be cast to nearly anything. So let’s determine how using dynamic is better than using System.Object?

As we discussed in part 1, in C#, the actual type behind the variable that is declared dynamic is resolved at runtime and a compiler simply assumes that the object in a variable declared dynamic just supports any operations (DO NOT CARE WHAT TO ASSIGN).

It means in dynamic, we can call a method on an object that is expected to be there at runtime, while in object type code will not get compiled if we use a method name of an object directly. To make it available, we need to use casting or reflection in order to work with object types.

Let’s take an example which clearly describes the difference between object and dynamictypes.

  1. Let's create a class for Emails and Enum for Email Type:
    /// <span class="code-SummaryComment"><summary>
    </span>/// Email information collection class
    /// <span class="code-SummaryComment"></summary>
    </span>public class Emails
    {
        /// <span class="code-SummaryComment"><summary>
    </span>    /// Email id
        /// <span class="code-SummaryComment"></summary>
    </span>    public Guid guid;
     
        /// <span class="code-SummaryComment"><summary>
    </span>    /// Email type
        /// <span class="code-SummaryComment"></summary>
    </span>    public EmailType emailType;
        
        /// <span class="code-SummaryComment"><summary>
    </span>    /// Email name
        /// <span class="code-SummaryComment"></summary>
    </span>    public string email;
      
        /// <span class="code-SummaryComment"><summary>
    </span>    /// Initializes instance of an Emails class
        /// <span class="code-SummaryComment"></summary>
    </span>    /// <span class="code-SummaryComment"><param name="guid">Email id</param>
    </span>    /// <span class="code-SummaryComment"><param name="emailType">Email type</param>
    </span>    /// <span class="code-SummaryComment"><param name="email">Email name</param>
    </span>    public Emails(Guid guid, EmailType emailType, string email)
        {
             this.guid = guid;
             this.emailType = emailType;
             this.email = email;
        }
    }
  2. Create an Enum of an Email Type that is used in the above class to get an EmailType.
    /// <span class="code-SummaryComment"><summary>
    </span>/// Email type
    /// <span class="code-SummaryComment"></summary>
    </span>public enum EmailType
    {
         /// <span class="code-SummaryComment"><summary>
    </span>     /// General
         /// <span class="code-SummaryComment"></summary>
    </span>     General,
        
         /// <span class="code-SummaryComment"><summary>
    </span>     /// Organization
         /// <span class="code-SummaryComment"></summary>
    </span>     Organization,
      
         /// <span class="code-SummaryComment"><summary>
    </span>     /// Collected
         /// <span class="code-SummaryComment"></summary>
    </span>     Collected
    }
  3. Main method which describes the original reference type object, System.Object object and dynamic object.
    static void Main(string[] args)
    {
    // Reference class (Emails) itself
    Emails emails = new Emails(new Guid(), 
    	EmailType.General, "sanjay.patolia@patni.com");
    Console.WriteLine("Email : {0}, Email Type : {1}, Email Id : {2}", 
    emails.email, emails.emailType, emails.guid);
     
    // Reference type object (Emails) to object type
    // Needs to explicit casting in order to make it work
    // Will be resolved at compile time.
    object objectEmails = new Emails(new Guid(), 
    	EmailType.General, "sanjay.patolia@patni.com");
    Console.WriteLine("Email : {0}, Email Type : {1}, Email Id : {2}", 
    (objectEmails as Emails).email, (objectEmails as Emails).emailType, 
    (objectEmails as Emails).guid);
     
    // Reference type object (Emails) to dynamic type
    // Use any member of Emails class
    // Will be resolved at run time.
    dynamic dynamicEmails = new Emails(new Guid(), 
    EmailType.General, "sanjay.patolia@patni.com");
    Console.WriteLine("Email : {0}, Email Type : {1}, Email Id : {2}", 
    dynamicEmails.email, dynamicEmails.emailType, dynamicEmails.guid);
     
    Console.ReadKey(true);
    }

In Main

Initially, we are going to create an object of a class Emails which is assigned to Emails itself and printing the member values of Emails by accessing the members using (.) Dot operator directly.

Secondly, we are creating an object of Emails class and assigning it to the object type and printing the member values of Emails class by casting the objectEmails from object type to Emails type. Here, we cannot directly say that (objectEmails.Email) this statement will not get compiled, because the member Email will not be available though it is assigned to objectEmails. We need to cast it to Emails type in order to make all the members of Emails class available, while this is not the case in dynamic.

In dynamic, as we discussed in part 1, the whole operation will be resolved at runtime, so we can assign or we can call any member of the Emails class, it will not generate a compile time error here. It checks at runtime whether the called method or member is available or not. It will throw an error at runtime, if it does not find the called member at runtime.

Array of Dynamic

dynamic[] emailsCollection = new dynamic[3];
Console.WriteLine(emailsCollection.GetType());

When we say, emailsCollection, it gives a list of members.

Now the question is why it shows members because dynamic does not show any members.

The answer is, it is an array of dynamic so it is not exactly a dynamic object, it is Sytem.Object[] when we declare the above dynamic statement.

Thus, an object type needs some reflection or explicit casting in order to make an object available. While in dynamic, runtime it checks the members, so we can call any members of assigned object without casting or any reflection method.

History

  • 19th April, 2011: Initial post

License

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

Share

About the Author

Sanjay J Patolia
Software Developer Software Industry
India India
No Biography provided

Comments and Discussions

 
General[My vote of 2] But Why? Pinmembercjb11019-Apr-11 21:21 
GeneralRe: [My vote of 2] But Why? PinmemberPatolia Sanjay27-Apr-11 5:40 

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 | Mobile
Web01 | 2.8.140814.1 | Last Updated 19 Apr 2011
Article Copyright 2011 by Sanjay J Patolia
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid