Understand C # series/Core C #/variables,

Source: Internet
Author: User

Understand C # series/Core C #/variables,
Variable variable?

A variable is used to specify a name for a thing. The function of a variable is similar to that of a person's name. When it comes to a name, you will know what it refers.

Variable type?

The variable type indicates the type of the variable. It indicates whether the variable is an integer, a decimal number, a character, an image, a human, or something.

Define Variables

[Variable type] [space] [variable name ]【;]

Assignment

[Variable name] [=] [value ]【;]

The first assignment of a variable is called variable initialization.

Variables must be assigned a value before they are used, that is, they must point to a target. They cannot only have names, but do not know what the names represent.

Assign values when defining variables

[Variable type] [space] [variable name] [=] [value ]【;]

Multiple values are initialized at the same time under the same variable type

[Variable type] [space] [variable name 1] [=] [value 1] [;] [variable name 2] [=] [value 2 ]【;]

When a variable is a field in a class or structure, if it is not initialized,. NET initializes the default value for the variable. When a variable is declared in a method, the compiler reports an error if it is not initialized. Universal variable type var?

The var type is inferred based on the variable initialization value. After the initialization is complete, the variable type is actually clear, and it is essentially a strong type.

Variables of the var type must be initialized. Otherwise, the compiler will not be able to deduce the variable type.

After the var type Initialization is complete, the variable type cannot be changed.

Scope of local variables?

The scope of a variable is the code area that can access the variable. The region is usually divided.

The Code Location for defining variables is different, and the scope is also different.

Variables with the same name cannot exist in a single scope. Otherwise, variables with the same name may exist in different scopes, and they do not interfere with each other.

When the local variables and fields are duplicated

A local variable hides a variable (field) defined under the class with the same name. A variable with the same name points to a local variable.

If you want to locally point to a field with the same name, you can add a special description (this) before the variable name, that is, [this.] [variable name]. this indicates the instance of this class. If the field is a global variable, replace this with the class name.

 

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.