A brief description of the difference between the keyword var and dynamic in C #

Source: Internet
Author: User

The difference between the keywords var and dynamic in C # is as follows:

1. Variables declared by Var must be initialized, and dynamic declared variables do not need to be initialized.

2, the VAR keyword can only declare local variables within the method, the dynamic keyword can be used for local variables, fields, parameters.

3. Expressions cannot be converted to Var, but can be transformed into dynamic.

4. When declaring a variable with the var keyword, the type of the variable is inferred at compile time based on the initialization value, and when the variable is declared with the dynamic keyword, the type check is not performed at compile time, and the object is only recognized at run time.

Note: Refer to "CLR via C # 4"

A brief description of the difference between the keyword var and dynamic in 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.