Analysis of dynamic and Var and object keywords of c#4.0 (EXT)

Source: Internet
Author: User

Http://www.cnblogs.com/Mainz/archive/2011/03/10/1980060.html

   1:  Dynamic a = 10;
   2:  a = a + 10;
   3:  Console.WriteLine (A.gettype ());

This code outputs System.Int32, and the second line does not require a type conversion because the type is recognized at run time. Dynamic uses the System.Object type in the background. However, unlike object, a dynamic type does not need to perform an explicit conversion operation at compile time because it recognizes the type only at run time. For a detailed distinction between dynamic and object, see "What is the difference between" dynamic "and" Object "keywords?"

In the C # type system, dynamic is actually a static type, which makes C # dynamic and still exists as a statically typed language. How to understand this sentence, it is necessary to understand how it is to achieve dynamic binding, see "dynamic Binding in c#4.0"

The following code will compile, but run the times wrong. Because when you use the dynamic keyword, you tell the compiler to turn off compile-time checking.

   1:  "Test";
   2:  a++;

To take a look at the Var keyword, starting with c#3.0, we are familiar with the Var of javascript and see C # var:

   1:  var a = 10;
   2:  A = a + 1;
   3:  Console.WriteLine (A.gettype ());

This code will enter System.Int32, the second line does not require a type conversion, and Var is strongly typed. When a variable is declared with the VAR keyword, the type of the variable is inferred at compile time based on the initialization string. You cannot change the type of the variable at run time.

Note, however, that the Var keyword is nothing more than an instruction that allows the compiler to infer a type based on the initialization expression of the variable, and VAR is not a type.

Finally, look at object, the keyword object, which represents the System.Object type, which is the root type in the C # class hierarchy. This keyword is often used when the object type cannot be determined at compile time, which often occurs in various interoperability scenarios.

But use object to avoid type conversions (explicit or implicit)

   1:   Object A = ten;
   2:   a = (int) A + ten;
   3:   "Test";

What does dynamic do for you?

With your imagination in mind, I can think of scripting support, such as office support for writing plugins with VBA scripts, which we can use to implement scriptable applications.

The second use is the dynamic method package, which is the object that can add and delete properties and methods at run time. The System.dynamic namespace is actually part of the DLR and can be used with the System.Dynamic.ExpandoObject and System.Expando.DynamicObject classes with the new Dynamic Key words to achieve their own needs.

A third use is to replace reflection. There is an example of invoking the Add method of a Calculator object by reflection, previously unaware of the type of calculator, only knowing that there is an Add method, the code is this:

   1:  Object calc = Getcalculator ();
   2:  Type calctype = Calc. GetType ();
   3:  object[] {ten, +});
   4:  int sum = Convert.ToInt32 (res);

Now without reflection, the code is concise with dynamic:

   1:  Dynamic calc = Getcalculator ();
   2:  int sum = Calc. ADD (ten);

It seems that dynamic is quite interesting.

Analysis of dynamic and Var and object keywords of c#4.0 (EXT)

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.