Declaration and initialization of variables in C #

Source: Internet
Author: User

C # Variable initialization is another example of C # 's emphasis on security. Simply put, the C # compiler needs to initialize the variable with an initial value before it can be referenced in the operation. Most modern compilers mark the absence of initialization as a warning, but the C # compiler treats it as an error.

1. In C #, the declared format of a variable is:

Data type variable name;

2. The variable's assignment format is:

  Variable name = data;

3. in general, all are declared after the assignment, or when declaring a variable and assigning the initial value. However, sometimes in the process of development and design, often forget to assign the initial value (that is, to initialize), which will lead to the design of the program, there will be unexpected errors.

Explanation: When we declare a variable, we make a memory allocation (such as int i; 4 bytes are allocated on the stack), that is, when the variable declaration is made, the memory is allocated to hold the value corresponding to that type. As for when we declare a variable and do not use it, this is the compiler will error, the wrong tip is: An unassigned variable was used. There is a concept in. NET that everyone is familiar with, and that is type safety. What is type safety, this question should be said to be a best example. If you are aware that in the. NET compiler, when using unassigned variables, the compiler will prompt this as an error, while the use of unassigned variables in other language compilers is prompted by a warning.

There are two ways to ensure type safety in 4.c#:

1) A variable is a field in a class or structure, and if it is not assigned, the value of the corresponding type is the default value when the variable is created in the default state.

2) The local variables of the method must be initialized in the code before they can be used in the statement. Initialization is not done when declaring a variable, and the compiler generates an error by examining all possible paths and if it detects that the local variable used its value before initialization.

1  Public Static void Main () 2 {3     int A; 4     Console.WriteLine (a); 5     return ; 6 }

The above code is reported in the compiler: use of unassigned local variable ' d ' ( using an unassigned local variable "a").

Although this protection mechanism is annoying, but in order to develop a good habit, or should be used when the change of face, the declaration of variables at the same time to initialize, so as not to bring yourself unnecessary trouble.

Declaration and initialization of variables 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.