I. BASIC syntax
1.c# are 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. The method either has no return value (void), or returns an integer (int)
Two. Variables
1. The compiler does not allow uninitialized variables to be used in an expression. If a variable is a field in a class or struct, if it is not initialized, the default value is 0 when the variables are created.
2. Type inference
type inference (inference) uses the var keyword. The compiler can "infer" the type of a variable based on the initialization value of the variable. var somenum = 0;
even if somenum is never declared int, the compiler can also determine that, as long as the somenum is within its scope, it is an int.
when you declare a variable and infer the type, you cannot change the type of the variable. After the type of the variable is determined, follow the strongly typed rules followed by the other variable types.
3. Scope of variables
The scope of a variable is the area of code that accesses the variable. As long as the scope of the variable is different parts of the program, there is no problem.
(1). Scope conflicts of local variables
public static int Main ()
{
Int J =20;
for (int i=0;i<20;i++)
{
int j =;
//Do something
}
}
compile error, because there are two variable J within the scope of the main () method, the compiler cannot distinguish between the two variables.
(2). Scope conflicts for fields and local variables
In some cases, you can distinguish between two identifiers with the same name and scope. The reason is that C # has a basic distinction between variables, which treat variables declared at the class level as
field, and the variable declared in the method is considered a local variable.
Class Program
{
int j =;
static void Main (string[] args)
{
int j =;
Console.WriteLine (j);
Console.readkey ();
}
}
Although two J is declared within the scope of the main () method, this code is compiled. J, defined at the class level, does not go out of scope before the class is deleted,
the J declared in Main () hides the class-level variable with the same name, so it is displayed when the code is run.
three. Constants
As the name implies, a constant is a variable whose value does not change during use. When declaring and initializing a variable, you can specify the variable by adding the keyword const before it
as a constant: const int a =100;
the characteristics of constants;
* Constants must be initialized at the time of declaration. You cannot overwrite a value after you specify it.
* The value of the constant must be used for calculation at compile time. Therefore, you cannot initialize constants by using values that are extracted from a variable. You should use Read-only fields (described later), if desired.
* constants are always static. Note, however, that it is not necessary (actually not allowed) to include static in a constant declaration
Benefits of using constants:
* Constants make programs easier to read because they replace harder-to-read numbers or strings with easy-to-read names (the value of the names is easy to understand).
* Constants make the program easy to modify. When the value of a constant needs to be modified, it is only necessary to assign the new value to the constant, without having to find the entire program to modify it.
* Constants make it easier to avoid program errors. If you assign a value to a constant outside the position where the constant is declared, the compiler will give an error.
There may be errors in the writing of the blog, welcome readers to criticize, email [email protected]
C # Basic Syntax (i)