Dynamic and VAR)

Source: Internet
Author: User

[C #4.0] the difference between dynamic and VaR

Note: The two keywords VaR and dynamic only look very similar.
That's all!

VaRIndicates"The variable type is determined during compilation.",DynamicTable
"The type of the variable is determined at runtime.". Therefore,DynamicAndVaRIt has different meanings.
VaRLet
If you enter less words when initializing a variable, the compiler will deduce the type of the Variable Based on the right value.DynamicEven worse, it tells the compiler that it simply ignores what type it is and runs
It is not too late.
VaRIt can only be used for the definition of local variables. You cannot define the attributes of a classVaRAnd cannot return the type or parameter of the method.
The data type is definedVaR.DynamicThere are no such limitations.
DynamicType does not skip type verification,
Delay to running. If type incompatibility is detected during running, an exception is thrown.

You may useDynamic:
1. COM Object
2. Dynamic Language (such as ironpython and ironruby) Objects
3. Reflection object
4. C #
Objects dynamically created in 4.0


When talking about dynamic, I first think of the VaR keyword. In fact, when var appeared in C #3.0, it caused some people to question. Later, Microsoft explained that VaR is only an implicit type declarative and can only be used as a local variable, in fact, it is still strongly typed, except that the compiler is inferred from the initialization result, so this variable can still be used with only prompts of. Dynamic is now a major step towards dynamic features, according to Anders's explanationDynamic is a dynamic static type, that is, it is still a static type in nature, but it reportsThe V. S. compiler ignores the static type check. It performs type check only at runtime. It can be applied to basically all C # types, such as methods and operators, indexer, attribute, and field.To call methods, attributes, and other operations in a unified manner.

Dynamic is mainly used to interact with external parties (such as COM, DLR, htmldom, and XML, you probably cannot determine the specific type of these objects, but only know some of its attributes, such as methods. Therefore, you only need to tell the compiler that you need to execute these methods here when the program runs, you may not care about what the operation object is. At this time, static types cannot help you solve the problem, because they are determined at compilation. reflection can be large, but it is too troublesome and inefficient. Therefore, dynamic emerged in a timely manner. It uses the missing cost of type check during compilation to make code that looks clean to programmers. The declaration and use of dynamic are simple, basically consistent with VAR in JavaScript. Note that we need to install vs2010 before compiling the code.

Beta2 or Visual C # express 2010, I installed C # express here (note 2 ).

E.g. Code 1:
Class
Program {
Static void main ()
{
Dynamic

A = 7;
A. Error = "error ";
A = "test ";
A. Run ();
}
}

This code can be compiled but cannot be run. C # The Compiler allows you to call any ?......

Com interoperability

C #4.0 contains multiple features, improving
Interoperability of API interfaces, such as office automation. Dynamic types, named parameters, and optional parameters are also part of the improvement.

Many com methods allow the parameter and return value types to be objects. Therefore, for a strongly typed language such as C #, a large amount of forced type conversion is required. However, in C #
4.0, if the/link option is added during compilation, the dynamic type will get a new effect: it makes the object type (parameter type or return type) in the COM interface method signature) it is regarded as dynamic, so it can avoid a lot of type conversion work. For example, the following statement is compared.

// Dynamic is not used.

(Excel. Range) excel. cells [1, 1]). value2 = "name ";

Excel. Range = (Excel. Range) excel. cells [1, 1];

// Dynamic is used,

Excel. cells [1, 1]. value = "name ";

Excel. Range = excel. cells [1, 1];

Category: C #

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.