In operations implemented through the dynamic type, this type is used to bypass the type check during compilation and parse these operations at runtime. The dynamic type simplifies access to com APIs (such as office automation APIs), dynamic APIs (such as the ironpython Library), and HTML document object models (DOM.In most cases, the dynamic type and object type have the same behavior. However, the compiler will not be used to parse or check operations that contain dynamic expression types. The compiler packs the information about the operation together and then uses the information for computing runtime operations. In this process, the variables of the Type Dynamic are compiled into the variables of the type object. Therefore, the Type Dynamic only exists during compilation and does not exist during runtime.
- CodeExample
-
- The Declaration is a type of attribute, field, indexer, parameter, return value, or type constraint. The following class definition uses dynamic in several different declarations.
class exampleclass { static dynamic field; dynamic prop { Get ; set ;}< span style =" color: blue; "> Public dynamic examplemethod ( dynamic d) { dynamic Local = " local variable "; int two = 2; If (d is int ) { return Local ;}< span style =" color: blue; "> else {< span style =" color: Blue; "> return two ;}}} |
-
- In explicit type conversion, It is the target type of conversion.
Static VoidConverttodynamic (){DynamicD;IntI = 20; D = (Dynamic) I; console. writeline (d );StringS ="Example string ."; D = (Dynamic) S; console. writeline (d); datetime dt = datetime. Today; D = (Dynamic) DT; console. writeline (d );} |
-
- Any context that acts as a value (such as the is operator or the right side of the as operator) as a type parameter or becomes part of the constructor type. For example, you can use dynamic in the following expressions.
IntI = 8;DynamicD; D = IAs Dynamic; Console. writeline (Typeof(List <Dynamic> )); |