Dynamic (C # Reference)

Source: Internet
Author: User


To learn about the latest documentation for Visual Studio RC, see the Visual Studio RC documentation.



In operations that are implemented by the dynamic type, the role of this type is to bypass compile-time type checking and resolve these operations at run time instead. The dynamic type simplifies access to COM APIs such as the Office Automation API, dynamic APIs (such as the IronPython Library), and the HTML Document Object Model (DOM).



In most cases, the dynamic type behaves the same as the object type. However, the compiler does not parse or type-check for operations that contain expressions of type dynamic. The compiler packages information about the operation, and the information is later used to calculate the run-time action. In this procedure, a variable of type dynamic is compiled into a variable of type object. Therefore, the type dynamic exists only at compile time and does not exist at run time.



The following example contrasts a variable of type dynamic with a variable of type object. To validate the type of each variable at compile time, place the mouse pointer over the dyn or obj in the WriteLine statement. IntelliSense shows Dyn's "dynamic" and "object" of obj.

class Program
    {static void Main (string [] args)
        {dynamic dyn = 1; object obj = 1; // Rest the mouse pointer over dyn and obj to see their
            // types at compile time.
            System.Console.WriteLine (dyn.GetType ());
            System.Console.WriteLine (obj.GetType ());
        }
    }
The WriteLine statement displays the runtime types of dyn and obj. At this time, both have the same integer type. Will produce the following output:

System.Int32

System.Int32

To see the difference between dyn and obj, add the following two lines between the declaration and WriteLine statement in the previous example.

dyn = dyn + 3;
obj = obj + 3;
Compiler errors are reported for attempts to add integers and objects in the expression obj + 3. However, dyn + 3 errors are not reported. The expression containing dyn is not checked at compile time because the type of dyn is dynamic.

Context

The dynamic keyword can appear directly or as a component of a constructed type in the following situations:

In the declaration, as the type of attribute, field, indexer, parameter, return value or type constraint The following class definition uses dynamic in several different declarations.

class ExampleClass
    {// A dynamic field.
        static dynamic field; // A dynamic property.
        dynamic prop {get; set;} // A dynamic return type and a dynamic parameter type.
        public dynamic exampleMethod (dynamic d)
        {// A dynamic local variable.
            dynamic local = "Local variable"; int two = 2; if (d is int)
            {return local;
            } else
            {return two;
            }
        }
    }
In explicit type conversion, as the target type of conversion.

 static void convertToDynamic ()
        {dynamic d; int i = 20;
            d = (dynamic) i;
            Console.WriteLine (d); string s = "Example string.";
            d = (dynamic) s;
            Console.WriteLine (d);

            DateTime dt = DateTime.Today;
            d = (dynamic) dt;
            Console.WriteLine (d);

        } // Results:
        // 20
        // Example string.
        // 2/17/2009 9:12:00 AM
In any context where a type acts as a value (such as the is operator or as operator to the right) or as a parameter of typeof becomes part of a constructed type For example, you can use dynamic in the following expressions.

        int i = 8; dynamic d; // With the is operator.
            // The dynamic type behaves like object. The following
            // expression returns true unless someVar has the value null.
            if (someVar is dynamic) {} // With the as operator.
            d = i as dynamic; // With typeof, as part of a constructed type.
            Console.WriteLine (typeof (List <dynamic>)); // The following statement causes a compiler error.
            //Console.WriteLine(typeof(dynamic));
Examples

The following example uses dynamic with multiple declarations. Main also uses runtime type checking versus compile-time type checking.

using System; namespace DynamicExamples
{class Program
    {static void Main (string [] args)
        {
            ExampleClass ec = new ExampleClass ();
            Console.WriteLine (ec.exampleMethod (10));
            Console.WriteLine (ec.exampleMethod ("value")); // The following line causes a compiler error because exampleMethod
            // takes only one argument.
            //Console.WriteLine(ec.exampleMethod(10, 4));

            dynamic dynamic_ec = new ExampleClass ();
            Console.WriteLine (dynamic_ec.exampleMethod (10)); // Because dynamic_ec is dynamic, the following call to exampleMethod
            // with two arguments does not produce an error at compile time.
            // However, itdoes cause a run-time error.
            //Console.WriteLine(dynamic_ec.exampleMethod(10, 4));
        }
    } class ExampleClass
    {static dynamic field; dynamic prop {get; set;} public dynamic exampleMethod (dynamic d)
        {dynamic local = "Local variable"; int two = 2; if (d is int)
            {return local;
            } else
            {return two;
            }
        }
    }
} // Results: // Local variable // 2 // Local variable

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.