C # Variable Type

Source: Internet
Author: User
Tags definition modifier range valid

In the C # language, we divide the variables into seven types, namely static variables (statically varibles), non-static variables (instance variables), array elements (array elements), value parameters (values parameters ), a reference parameter (reference parameters), an output parameter (outputs parameters), and a local variable (regional variables). Look at the following example:

Class A
{public
  static int x;
  int y;
  void F (int[] v,int a,ref int b,out int c) {
       int i=1;
       c=a+b++
  }
}

In the above variable declaration, X is a static variable, y is a non-static variable, v[0] is an array element, A is a value parameter, B is a reference parameter, C is an output parameter, and I is a local variable.

static variables

Variables declared with the "static" modifier are called static variables. Once the class to which the static variable belongs is loaded, it will persist until the program that contains the class has finished running. The initial value of a static variable is the default value for that variable type. For easy definition of assignment checking, static variables are best assigned when defined. Such as:

static int a=0;

Non-static variables

A declaration without a "static" modifier declaration is called an instance variable. Such as:

int A;

For non-static variables in a class, the non-static variable will persist once a new instance of a class is created until the instance is no longer applied and the space in which it resides is freed. Also, given the definition of assignment checking, a non-static variable of a class should also be assigned at initialization time.

Non-static variables in the structure exist with the existence of the structure. In other words, when a variable of a struct type exists or ends, the variable in that struct type also exists and ends. Similarly, an instance variable in a struct is initialized with the initialization of the struct type variable, and if the struct type variable is not assigned when it is defined, then the instance variable is not assigned to the value.

Array elements

An array element is also one of the variables that exists with the existence of an array instance. The initial value of each array element is the default value of the element type for that group. Also, given the definition of assignment checking, array elements are best evaluated at the initial time.

Local variables

A local variable is a variable that is declared in a separate program block, a for statement, a switch statement, or a using statement, and it is only valid in that scope. When the program runs to this range, the variable begins to take effect, and the variable becomes invalid when the program leaves.

Unlike several other variable types, local variables are not automatically initialized, so there is no default value. When an assignment check is performed, the local variable is considered not to be assigned a value.

In the valid range of a local variable, it is illegal to use it before the definition of a variable, such as:

for (int i=0;i<10;i++) {
int num=a; Illegal because the local variable A is not yet defined

int A;
int b=a; That's right
}

The sentencing value parameter, the reference parameter, the output parameter, we put in the 11th chapter "method" to carry on the detailed introduction.

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.