Constants, type inferences, and scopes in C #

Source: Internet
Author: User

I. Constants

A constant is a variable whose value does not change during use. Keywords in front of the variable during declaration and initialization

Const int a = 100; //

Constant features:

Using constants in a program must include at least

For example:

  j=  time=   j=

 

Errors generated after compilation:

For

Both constants and read-only constants can only be accessed and cannot be modified. However, their assignment time is not the same. Generally, constants are determined during compilation and assigned constant values. Read-Only is actually a variable that will be assigned a value only when it needs to be dynamically loaded during runtime, and this value cannot be changed once it is assigned.

Ii. type inference

Use of type inference

Int someNumber = 0;

It becomes

Var someNumber = 0;

Even if

The following is another example:

  Main( name = age= isRabbit====+++

Compile and run the program:

Name is type System. String

Age is type System. Int32

IsRabbit is type System. Boolean

Use

The scope of a variable is the code area that can access the variable. In general, make sure that the scope has the following rules:

It is common to provide the same variable name for different variables in different parts of a large program. As long as the variable scope is different parts of the program, there will be no problem. And will not produce fuzzy. Note that local variables with the same name cannot be declared twice in the same scope, so the following code cannot be used:

Int x = 20;

Int x = 30;

Let's take a look at the following example:

  i=;i<;i++ i=;i>=

 

This code should be noted. I appears twice, but they are all variables relative to the loop body.

Another example:

   j=( i=;i<;i++ j=;    Console.WriteLine(j+ 

 

Field or local variable scope conflict: in some cases, the same name can be distinguished

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.