Analysis of Dynamic, VAR, and object keywords in C #4.0

Source: Internet
Author: User

LookCode:

 
1:Dynamic A = 10;
 
2:A = a + 10;
 
3:Console. writeline (A. GetType ());

This code segment outputs system. int32, and the second line does not require type conversion, because the type is recognized at runtime. Dynamic uses the system. Object Type in the background. However, unlike objects, dynamic types do not need to be explicitly converted during compilation because they only recognize types at runtime. For detailed differences between dynamic and object, see what is the difference between "dynamic" and "object" keywords?

In a C # type system, dynamic is actually a static type. This keyword enables C # To Obtain Dynamic functions and still exist as a static typed language. To understand this sentence, we need to understand how it implements dynamic binding. Let's look at dynamic binding in C #4.0.

The following code is compiled, but an error is reported during running. Because when you use the dynamic keyword, you tell the compiler to disable the compile-time check.

 
1:Dynamic A ="Test";
 
2:A ++;

Let's take a look at the VaR keyword. From the beginning of C #3.0, we are familiar with JavaScript VaR and look at the VaR of C:

 
1:VaR A = 10;
 
2:A = a + 1;
 
3:Console. writeline (A. GetType ());

This code segment will enter system. int32. The second line does not require type conversion, and VaR is strongly typed. When the VaR keyword is used to declare a variable, the variable type will be inferred based on the initialization string during compilation. The variable type cannot be changed at runtime.

Note that the VaR keyword is just a command that allows the compiler to deduce the type based on the variable's initialization expression; var is not a type.

Finally, let's take a look at the object. The keyword object indicates 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 during compilation, which often occurs in various interoperability scenarios.

However, to use an object, avoid type conversion (explicit or implicit)

1: ObjectA = 10;
 
2:A = (Int) A + 10;
 
3:A ="Test";

What can dynamic do?

What I can think of from your imagination is that it can be used for script support. For example, if office support uses VBA scripts to write plug-ins, we can also use this application to implement scripting.

The second use is to use a dynamic method package. A dynamic method package is an object that allows you to add and delete attributes and methods at runtime. The system. Dynamic namespace is actually part of the DLR. You can use the system. Dynamic. expandoobject and system. expando. dynamicobject classes to combine with the new dynamic keywords to implement what you need.

The third use is to replace reflection. In another example, we call the add method of a calculator object through reflection. We did not know the type of the calculator before, but only knew that there was an add method. The Code is as follows:

 
1:ObjectCalc = getcalculator ();
 
2:Type calctype = Calc. GetType ();
 
3:ObjectRes = calctype. invokemember ("Add", Bindingflags. invokemethod,Null,New Object[] {10, 20 });
 
4:IntSum = convert. toint32 (RES );

Now the code is concise without using dynamic for reflection:

 
1:Dynamic calc = getcalculator ();
 
2:IntSum = Calc. Add (10, 20 );

In this case, dynamic is quite interesting.

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.