|  | 
| 1 | 1 | --- | 
| 2 |  | -title: "Using Type dynamic (C# Programming Guide)" | 
|  | 2 | +title: "Using type dynamic (C# Programming Guide)" | 
| 3 | 3 | ms.date: 07/20/2015 | 
| 4 | 4 | helpviewer_keywords:  | 
| 5 | 5 |   - "dynamic [C#], about dynamic type" | 
| 6 | 6 |   - "dynamic type [C#]" | 
| 7 | 7 | ms.assetid: 3828989d-c967-4a51-b948-857ebc8fdf26 | 
| 8 | 8 | --- | 
| 9 |  | -# Using Type dynamic (C# Programming Guide) | 
| 10 |  | -[!INCLUDE[csharp_dev10_long](~/includes/csharp-dev10-long-md.md)] introduces a new type, `dynamic`. The type is a static type, but an object of type `dynamic` bypasses static type checking. In most cases, it functions like it has type `object`. At compile time, an element that is typed as `dynamic` is assumed to support any operation. Therefore, you do not have to be concerned about whether the object gets its value from a COM API, from a dynamic language such as IronPython, from the HTML Document Object Model (DOM), from reflection, or from somewhere else in the program. However, if the code is not valid, errors are caught at run time.   | 
| 11 |  | -   | 
| 12 |  | - For example, if instance method `exampleMethod1` in the following code has only one parameter, the compiler recognizes that the first call to the method, `ec.exampleMethod1(10, 4)`, is not valid because it contains two arguments. The call causes a compiler error. The second call to the method, `dynamic_ec.exampleMethod1(10, 4)`, is not checked by the compiler because the type of `dynamic_ec` is `dynamic`. Therefore, no compiler error is reported. However, the error does not escape notice indefinitely. It is caught at run time and causes a run-time exception.   | 
| 13 |  | -   | 
| 14 |  | - [!code-csharp[CsProgGuideTypes#50](../../../csharp/programming-guide/nullable-types/codesnippet/CSharp/using-type-dynamic_1.cs)]   | 
| 15 |  | -   | 
| 16 |  | - [!code-csharp[CsProgGuideTypes#56](../../../csharp/programming-guide/nullable-types/codesnippet/CSharp/using-type-dynamic_2.cs)]   | 
| 17 |  | -   | 
| 18 |  | - The role of the compiler in these examples is to package together information about what each statement is proposing to do to the object or expression that is typed as `dynamic`. At run time, the stored information is examined, and any statement that is not valid causes a run-time exception.   | 
| 19 |  | -   | 
| 20 |  | - The result of most dynamic operations is itself `dynamic`. For example, if you rest the mouse pointer over the use of `testSum` in the following example, IntelliSense displays the type **(local variable) dynamic testSum**.   | 
| 21 |  | -   | 
| 22 |  | - [!code-csharp[CsProgGuideTypes#51](../../../csharp/programming-guide/nullable-types/codesnippet/CSharp/using-type-dynamic_3.cs)]   | 
| 23 |  | -   | 
| 24 |  | - Operations in which the result is not `dynamic` include: | 
| 25 |  | -  | 
| 26 |  | -* Conversions from `dynamic` to another type.  | 
| 27 |  | -* Constructor calls that include arguments of type `dynamic`.  | 
|  | 9 | +# Using type dynamic (C# Programming Guide) | 
|  | 10 | + | 
|  | 11 | +[!INCLUDE[csharp_dev10_long](~/includes/csharp-dev10-long-md.md)] introduces a new type, `dynamic`. The type is a static type, but an object of type `dynamic` bypasses static type checking. In most cases, it functions like it has type `object`. At compile time, an element that is typed as `dynamic` is assumed to support any operation. Therefore, you do not have to be concerned about whether the object gets its value from a COM API, from a dynamic language such as IronPython, from the HTML Document Object Model (DOM), from reflection, or from somewhere else in the program. However, if the code is not valid, errors are caught at run time. | 
|  | 12 | + | 
|  | 13 | +For example, if instance method `exampleMethod1` in the following code has only one parameter, the compiler recognizes that the first call to the method, `ec.exampleMethod1(10, 4)`, is not valid because it contains two arguments. The call causes a compiler error. The second call to the method, `dynamic_ec.exampleMethod1(10, 4)`, is not checked by the compiler because the type of `dynamic_ec` is `dynamic`. Therefore, no compiler error is reported. However, the error does not escape notice indefinitely. It is caught at run time and causes a run-time exception. | 
|  | 14 | + | 
|  | 15 | +[!code-csharp[CsProgGuideTypes#50](~/samples/snippets/csharp/VS_Snippets_VBCSharp/CsProgGuideTypes/CS/usingdynamic.cs#50)] | 
|  | 16 | + | 
|  | 17 | +[!code-csharp[CsProgGuideTypes#56](~/samples/snippets/csharp/VS_Snippets_VBCSharp/CsProgGuideTypes/CS/usingdynamic.cs#56)] | 
|  | 18 | + | 
|  | 19 | +The role of the compiler in these examples is to package together information about what each statement is proposing to do to the object or expression that is typed as `dynamic`. At run time, the stored information is examined, and any statement that is not valid causes a run-time exception. | 
|  | 20 | + | 
|  | 21 | +The result of most dynamic operations is itself `dynamic`. For example, if you rest the mouse pointer over the use of `testSum` in the following example, IntelliSense displays the type **(local variable) dynamic testSum**. | 
|  | 22 | + | 
|  | 23 | +[!code-csharp[CsProgGuideTypes#51](~/samples/snippets/csharp/VS_Snippets_VBCSharp/CsProgGuideTypes/CS/usingdynamic.cs#51)] | 
|  | 24 | + | 
|  | 25 | +Operations in which the result is not `dynamic` include: | 
|  | 26 | + | 
|  | 27 | +* Conversions from `dynamic` to another type. | 
|  | 28 | +* Constructor calls that include arguments of type `dynamic`. | 
| 28 | 29 | 
 | 
| 29 | 30 | For example, the type of `testInstance` in the following declaration is `ExampleClass`, not `dynamic`: | 
| 30 |  | -   | 
| 31 |  | - [!code-csharp[CsProgGuideTypes#52](../../../csharp/programming-guide/nullable-types/codesnippet/CSharp/using-type-dynamic_4.cs)]   | 
| 32 |  | -   | 
| 33 |  | - Conversion examples are shown in the following section, "Conversions."   | 
| 34 |  | -   | 
| 35 |  | -## Conversions   | 
| 36 |  | - Conversions between dynamic objects and other types are easy. This enables the developer to switch between dynamic and non-dynamic behavior.   | 
| 37 |  | -   | 
| 38 |  | - Any object can be converted to dynamic type implicitly, as shown in the following examples.   | 
| 39 |  | -   | 
| 40 |  | - [!code-csharp[CsProgGuideTypes#53](../../../csharp/programming-guide/nullable-types/codesnippet/CSharp/using-type-dynamic_5.cs)]   | 
| 41 |  | -   | 
| 42 |  | - Conversely, an implicit conversion can be dynamically applied to any expression of type `dynamic`.   | 
| 43 |  | -   | 
| 44 |  | - [!code-csharp[CsProgGuideTypes#54](../../../csharp/programming-guide/nullable-types/codesnippet/CSharp/using-type-dynamic_6.cs)]   | 
| 45 |  | -   | 
| 46 |  | -## Overload Resolution with Arguments of Type dynamic   | 
| 47 |  | - Overload resolution occurs at run time instead of at compile time if one or more of the arguments in a method call have the type `dynamic`, or if the receiver of the method call is of type `dynamic`. In the following example, if the only accessible `exampleMethod2` method is defined to take a string argument, sending `d1` as the argument does not cause a compiler error, but it does cause a run-time exception. Overload resolution fails at run time because the run-time type of `d1` is `int`, and `exampleMethod2` requires a string.   | 
| 48 |  | -   | 
| 49 |  | - [!code-csharp[CsProgGuideTypes#55](../../../csharp/programming-guide/nullable-types/codesnippet/CSharp/using-type-dynamic_7.cs)]   | 
| 50 |  | -   | 
| 51 |  | -## Dynamic Language Runtime   | 
| 52 |  | - The dynamic language runtime (DLR) is a new API in [!INCLUDE[net_v40_short](~/includes/net-v40-short-md.md)]. It provides the infrastructure that supports the `dynamic` type in C#, and also the implementation of dynamic programming languages such as IronPython and IronRuby. For more information about the DLR, see [Dynamic Language Runtime Overview](../../../framework/reflection-and-codedom/dynamic-language-runtime-overview.md).   | 
| 53 |  | -   | 
| 54 |  | -## COM Interop   | 
| 55 |  | - [!INCLUDE[csharp_dev10_long](~/includes/csharp-dev10-long-md.md)] includes several features that improve the experience of interoperating with COM APIs such as the Office Automation APIs. Among the improvements are the use of the `dynamic` type, and of [named and optional arguments](../../../csharp/programming-guide/classes-and-structs/named-and-optional-arguments.md).   | 
| 56 |  | -   | 
| 57 |  | - Many COM methods allow for variation in argument types and return type by designating the types as `object`. This has necessitated explicit casting of the values to coordinate with strongly typed variables in C#. If you compile by using the [/link (C# Compiler Options)](../../../csharp/language-reference/compiler-options/link-compiler-option.md) option, the introduction of the `dynamic` type enables you to treat the occurrences of `object` in COM signatures as if they were of type `dynamic`, and thereby to avoid much of the casting. For example, the following statements contrast how you access a cell in a Microsoft Office Excel spreadsheet with the `dynamic` type and without the `dynamic` type.   | 
| 58 |  | -   | 
| 59 |  | - [!code-csharp[csOfficeWalkthrough#12](../../../csharp/programming-guide/interop/codesnippet/CSharp/using-type-dynamic_8.cs)]   | 
| 60 |  | -   | 
| 61 |  | - [!code-csharp[csOfficeWalkthrough#13](../../../csharp/programming-guide/interop/codesnippet/CSharp/using-type-dynamic_9.cs)]   | 
| 62 |  | -   | 
| 63 |  | -## Related Topics   | 
| 64 |  | -   | 
| 65 |  | -|Title|Description|   | 
| 66 |  | -|-----------|-----------------|   | 
| 67 |  | -|[dynamic](../../../csharp/language-reference/keywords/dynamic.md)|Describes the usage of the `dynamic` keyword.|   | 
| 68 |  | -|[Dynamic Language Runtime Overview](../../../framework/reflection-and-codedom/dynamic-language-runtime-overview.md)|Provides an overview of the DLR, which is a runtime environment that adds a set of services for dynamic languages to the common language runtime (CLR).|   | 
| 69 |  | -|[Walkthrough: Creating and Using Dynamic Objects](../../../csharp/programming-guide/types/walkthrough-creating-and-using-dynamic-objects.md)|Provides step-by-step instructions for creating a custom dynamic object and for creating a project that accesses an `IronPython` library.|   | 
| 70 |  | -|[How to: Access Office Interop Objects by Using Visual C# Features](../../../csharp/programming-guide/interop/how-to-access-office-onterop-objects.md)|Demonstrates how to create a project that uses named and optional arguments, the `dynamic` type, and other enhancements that simplify access to Office API objects.| | 
|  | 31 | + | 
|  | 32 | +[!code-csharp[CsProgGuideTypes#52](~/samples/snippets/csharp/VS_Snippets_VBCSharp/CsProgGuideTypes/CS/usingdynamic.cs#52)] | 
|  | 33 | + | 
|  | 34 | +Conversion examples are shown in the following section, "Conversions." | 
|  | 35 | + | 
|  | 36 | +## Conversions | 
|  | 37 | + | 
|  | 38 | +Conversions between dynamic objects and other types are easy. This enables the developer to switch between dynamic and non-dynamic behavior. | 
|  | 39 | + | 
|  | 40 | +Any object can be converted to dynamic type implicitly, as shown in the following examples. | 
|  | 41 | + | 
|  | 42 | +[!code-csharp[CsProgGuideTypes#53](~/samples/snippets/csharp/VS_Snippets_VBCSharp/CsProgGuideTypes/CS/usingdynamic.cs#53)] | 
|  | 43 | + | 
|  | 44 | +Conversely, an implicit conversion can be dynamically applied to any expression of type `dynamic`. | 
|  | 45 | + | 
|  | 46 | +[!code-csharp[CsProgGuideTypes#54](~/samples/snippets/csharp/VS_Snippets_VBCSharp/CsProgGuideTypes/CS/usingdynamic.cs#54)] | 
|  | 47 | + | 
|  | 48 | +## Overload resolution with arguments of type dynamic | 
|  | 49 | + | 
|  | 50 | +Overload resolution occurs at run time instead of at compile time if one or more of the arguments in a method call have the type `dynamic`, or if the receiver of the method call is of type `dynamic`. In the following example, if the only accessible `exampleMethod2` method is defined to take a string argument, sending `d1` as the argument does not cause a compiler error, but it does cause a run-time exception. Overload resolution fails at run time because the run-time type of `d1` is `int`, and `exampleMethod2` requires a string. | 
|  | 51 | + | 
|  | 52 | +[!code-csharp[CsProgGuideTypes#55](~/samples/snippets/csharp/VS_Snippets_VBCSharp/CsProgGuideTypes/CS/usingdynamic.cs#55)] | 
|  | 53 | + | 
|  | 54 | +## Dynamic language runtime | 
|  | 55 | + | 
|  | 56 | +The dynamic language runtime (DLR) is a new API in [!INCLUDE[net_v40_short](~/includes/net-v40-short-md.md)]. It provides the infrastructure that supports the `dynamic` type in C#, and also the implementation of dynamic programming languages such as IronPython and IronRuby. For more information about the DLR, see [Dynamic Language Runtime Overview](../../../framework/reflection-and-codedom/dynamic-language-runtime-overview.md). | 
|  | 57 | + | 
|  | 58 | +## COM interop | 
|  | 59 | + | 
|  | 60 | +[!INCLUDE[csharp_dev10_long](~/includes/csharp-dev10-long-md.md)] includes several features that improve the experience of interoperating with COM APIs such as the Office Automation APIs. Among the improvements are the use of the `dynamic` type, and of [named and optional arguments](../classes-and-structs/named-and-optional-arguments.md). | 
|  | 61 | + | 
|  | 62 | +Many COM methods allow for variation in argument types and return type by designating the types as `object`. This has necessitated explicit casting of the values to coordinate with strongly typed variables in C#. If you compile by using the [/link (C# Compiler Options)](../../../csharp/language-reference/compiler-options/link-compiler-option.md) option, the introduction of the `dynamic` type enables you to treat the occurrences of `object` in COM signatures as if they were of type `dynamic`, and thereby to avoid much of the casting. For example, the following statements contrast how you access a cell in a Microsoft Office Excel spreadsheet with the `dynamic` type and without the `dynamic` type. | 
|  | 63 | + | 
|  | 64 | +[!code-csharp[csOfficeWalkthrough#12](~/samples/snippets/csharp/VS_Snippets_VBCSharp/csofficewalkthrough/cs/thisaddin.cs#12)] | 
|  | 65 | + | 
|  | 66 | +[!code-csharp[csOfficeWalkthrough#13](~/samples/snippets/csharp/VS_Snippets_VBCSharp/csofficewalkthrough/cs/thisaddin.cs#13)] | 
|  | 67 | + | 
|  | 68 | +## Related topics | 
|  | 69 | + | 
|  | 70 | +|Title|Description| | 
|  | 71 | +|-----------|-----------------| | 
|  | 72 | +|[dynamic](../../language-reference/keywords/dynamic.md)|Describes the usage of the `dynamic` keyword.| | 
|  | 73 | +|[Dynamic Language Runtime Overview](../../../framework/reflection-and-codedom/dynamic-language-runtime-overview.md)|Provides an overview of the DLR, which is a runtime environment that adds a set of services for dynamic languages to the common language runtime (CLR).| | 
|  | 74 | +|[Walkthrough: Creating and Using Dynamic Objects](walkthrough-creating-and-using-dynamic-objects.md)|Provides step-by-step instructions for creating a custom dynamic object and for creating a project that accesses an `IronPython` library.| | 
|  | 75 | +|[How to: Access Office Interop Objects by Using Visual C# Features](../interop/how-to-access-office-onterop-objects.md)|Demonstrates how to create a project that uses named and optional arguments, the `dynamic` type, and other enhancements that simplify access to Office API objects.| | 
0 commit comments