C # basic syntax (1 ),

Source: Internet
Author: User
Tags variable scope

C # basic syntax (1 ),

I. Basic syntax

1. C # Is case sensitive, so myVar and MyVar are two different variables.
2. Each C # executable file (such as console applications, Windows applications, and Windows Services) must have an entry point ---- Main () (M capital)
Public static void Main ()
{
// Do something
}
This method is called when the program starts. This method either has no return value (void) or returns an integer (int)


2. Variables

1. the compiler cannot use uninitialized variables in expressions. If the variables are fields in a class or structure, if they are not initialized, the default value is 0 when these variables are created.
2. type inference
Type inference uses the var keyword. The compiler can "deduce" the type of the variable based on its initialization value. Var somenum = 0;
Even if somenum is never declared as an int, the compiler can determine that as long as somenum is within its scope, it is an int.
After declaring a variable and inferring the type, the variable type cannot be changed. After the variable type is determined, it follows the strongly typed rules followed by other variable types.
3. Scope of Variables
The scope of a variable is the code area that accesses the variable. As long as the variable scope is different parts of the program, there will be no problem.
(1). Scope conflict of local variables
Public static int Main ()
{
Int j = 20;
For (int I = 0; I <20; I ++)
{
Int j = 30;
// Do something
}
}
An error is reported during compilation because there are two variables j in the Main () method scope, which cannot be distinguished by the compiler.

(2). The field conflicts with the local variable scope.
In some cases, two identifiers with the same name and same scope can be distinguished. The reason is that C # has a basic distinction between variables. It regards variables declared at the class level
The variable declared in the method is considered as a local variable.
Class Program
{
Int j = 20;
Static void Main (string [] args)
{
Int j = 30;
Console. WriteLine (j );
Console. ReadKey ();
}
}
Although two j are declared in the scope of the Main () method, this code will also be compiled. The j defined at the class level does not go beyond the scope before the class is deleted.
The j declared in Main () hides class-level variables of the same name, so 30 is displayed when you run the code.

3. Constants
As the name suggests, a constant is a variable whose value does not change during use. When declaring and initializing a variable, add the keyword const before the variable to specify the variable
Is a constant: const int a = 100;

Characteristics of constants;
* Constants must be initialized during declaration. Once the value is specified, it cannot be rewritten.
* The constant value must be used for computation during compilation. Therefore, it cannot be used to initialize Constants by extracting values from a variable. If necessary, use the read-only field (described later ).
* Constants are always static. But note that you do not have to (in fact not allowed) include static in the constant declaration.

Benefits of using constants:
* Because the easy-to-read name (the value of the name is easy to understand) replaces the hard-to-read number or string, constants make the program easier to read.
* Constants make the program easy to modify. When the value of a constant needs to be modified, you only need to assign the new value to this constant, and you do not have to look for the entire program to modify it.
* Constants are easier to avoid program errors. If a constant is assigned a value other than the position where the constant is declared, the compiler reports an error.

 

 

Blog may be written in the error, welcome readers to correct criticism, mailbox 15734108350@163.com.

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.