Implicitly-typed Local variables

Source: Internet
Author: User

In general, we declare variables by specifying the data type and identifier, as follows:
int myint=32;
You can also define it as follows,
int myint=myotherint*99; (Myotherint has been initialized)
In addition, you can ask the C # compiler to judge the type of a variable based on an expression, using the keyword VAR to declare it, as
var myvariance=99;
var myothervariance= "Hello";
Two variables myvariance and myothervariance calledImplicit type(Implicitly typed) variable. The VAR keyword instructs the compiler to infer the type of the variable based on the expression used to initialize it. In the previous example, the myvariance is an int type, and in the following example, the Myothervariance is a string type.
Notable points:
①,var only gives us the convenience of the variable declaration, once the declaration is finished, only the type of value inferred by the compiler can be assigned to him, for example, you cannot assign a float, double, string type to myvariable;
②, the keyword var can only be used when an expression is provided to initialize the variable, and the following declaration is illegal.
var yetanothervariable, not initialized
  ③,var encourages programmers to be lazy and makes programs more difficult to understand; In general, you should insist on using a variable of the type explicitly, unless you have to, do not use an implicit variable.

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.