Starting from this section, we will review the basic knowledge of C #, including variable declaration, initialization of variables, scope, pre-defined data types of C #, loops and condition statements in C, enumeration, namespace, main () method, C # identifier and keyword, C # encoding specifications and conventions, and so on.
First, we start from the very classic "Hello word !" Start.
Create a new. txt file and change it to test. CS. Enter the following content.
Code
code highlighting produced by actipro codehighlighter (freeware)
http://www.CodeHighlighter.com/
--> using system;
namespace gosoa.com
{< br> class myfir Stclass
{< br> static void main ()
{< br> console. writeline ( " Hello world! " );
}< BR >}< br>
Open the C # command line compiler and compile the file. That is, enter
CSC test. CS
After successful compilation, the Directory of the file will be stored and a test.exe file will be generated. After we input test.exe in the csung command line, the test.exe file will be run and the Hello world! (Note: here the C # command line is at the beginning-Program-Microsoft. NET Framework SDK V2.0-SDK command prompt)
In the previous example, we briefly described several precautions. First, the first sentence using system; is used to introduce the system base class. Similar to Java import, This is the base class of C #. All the work of C # depends on this base class. Namespace gosoa.com is the namespace we mentioned earlier. The namespace is gosoa.com. Of course, you can name it as any name. However, to avoid conflicts, we generally use our own company's domain name as the namespace. Third, class myfirstclass declares a class named myfirstclass. Fourth, static void
Main () is the main method of the program. Note that the first letter of the main () method is capitalized here. Fifth, console. writeline ("Hello world! "); Is to output Hello world,
Console. writeline is a method in the base class.
In C #, like many other languages (C, Java, etc.), the end of a sentence is a semicolon (;).CodePut them in braces.
I. Variables
1.1 variable Declaration
The declaration of variables in C # is illustrated by examples. For example, int I; this statement declares an int (integer) variable I. Another example is string STR; which declares a string (string type)
Variable Str.
1.2 variable Initialization
C # The Compiler requires that each variable be used after the initial value.
Note the following when initializing C # VARIABLES,
A. variables are fields in a class or structure. If no Explicit initialization is performed, the initial value of these variables is 0 by default. For example, the following code:
Code
UsingSystem;
NamespaceGosoa.com
{
ClassMyfirstclass
{
Static IntY;
Static VoidMain ()
{
Console. writeline (y );
}
}
}
We declare a variable Y in the class, and then output the variable. After compilation and running, we will see that the output result is 0.
B. the variables in the method must be explicitly initialized. Otherwise, errors may occur when the variable is used. The following code: an error is reported during compilation. We need to explicitly initialize int y;
. For example, if we initialize the value of Y to 10, that is, int y = 10, it will be compiled.
Code
code highlighting produced by actipro codehighlighter (freeware)
http://www.CodeHighlighter.com/
--> using system;
namespace gosoa.com
{< br> class myfirstclass
{< br> static void main ()
{< br> int Y;
console. writeline (y);
}< BR >}< br>
1.3Variable Scope
The scope of a variable refers to the code area where the variable can be used. In general, it is determined that the scope has the following rules.
A, The field (also called Member variables) are also in this scope.
B,Local variables exist in the scope before the block statement or method braces that declare the variable end.
C, in for , while variables declared in a loop exist only in the loop.
When using variables, naming conflicts may occur. First, let's look at the scope conflict of local variables. Example code:
Code
Using System;
Namespace Gosoa.com
{
Class Myfirstclass
{
Static Void Main ()
{
For ( Int I = 0 ; I < 10 ; I ++ )
{
Console. writeline (I );
}
For ( Int I = 0 ; I < 20 ; I ++ )
{
Console. writeline (I );
}
}
}
}
Both loops are used.IBut can be output normally, because eachIThe scopes are in the corresponding two loops.
Let's look at the following code:
Code
Using System;
Namespace Gosoa.com
{
Class Myfirstclass
{
Static Void Main ()
{
Int J = 5 ;
For ( Int I = 0 ; I < 10 ; I ++ )
{
Int J = 20 ;
Console. writeline (I + J );
}
}
}
}
This code compilation will cause errors, because the firstJThe scope is the wholeMain ()Method. Therefore, define a circular body with the same nameJThe error will be reported.
Let's look at the following sample code,
Code
Using System;
Namespace Gosoa.com
{
Class Myfirstclass
{
Int J = 30 ;
Static Void Main ()
{
Int J = 20 ;
Int I = 5 ;
Console. writeline (I + J );
}
}
}
in this code, first j the scope is the entire class, that is, the class field, second j the statement replaces the first j , therefore, the program outputs 25.
1.4Constant
When declaring a variable, addConstKeyword to specify this variable as a constant.
Pay attention to the following points,
AA constant must be initialized when it is declared and cannot be changed after it is assigned a value.
BConstant is always static(Static)Does not have to be added when declaring constants.StaticKeyword.