. NET data type-dynamic type (dynamic)

Source: Internet
Author: User

 

    • Dynamic

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> ));

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.