C # basic syntax (2 ),
Iv. CTS type
The basic predefined types recognized by C # are not built into the C # language, but are built into the. NET Framework.
For example, when C # declares an int type data, it is actually an instance of the. NET structure System. Int32. This means that all the basic data types can be syntactically
Class that supports some methods.
Convert int I to string, string s = I. ToString ();
Behind this convenient syntax, types are actually stored as basic types. The basic types are represented in the. NET structure in concept, so there is no performance loss.
C # has 15 predefined types. string and object are reference types.
5. Enumeration
Public enum TimeDay
{
Morning = 0,
Afternoon = 1,
Evening = 2
}
1. enumeration is a user-defined Integer type.
2. In the long run, creating enumeration can save a lot of time and reduce a lot of trouble. Advantages of enumeration over integers:
* Enumeration makes the code easier to maintain and helps to specify valid and expected values for the variable;
* Enumeration makes the code clearer. descriptive names are used to represent integer values, rather than fuzzy meanings and variable numbers;
3. in C #, the true strength of enumeration is that it will be instantiated in the background as a structure derived from the base class System. Enum. This indicates that you can call methods to execute useful tasks.
TimeDay td = TimeDay. Afternoon;
Console. WriteLine (td. ToString ());
Note that because of the execution method of. NET Framework, taking enumeration as a structure in syntax will not cause performance loss. In fact, once the code is compiled, enumeration becomes the basic type,
Similar to int and float.
4. Obtain the enumerated value from the string
TimeDay td = (TimeDay) Enum. Parse (typeof (TimeDay), "afternoon", true );
Console. WriteLine (int) td );
To convert a string, you must use the static method Enum. Parse. The first parameter is the enumeration type to be used, the second parameter is the string to be converted, and the third parameter is specified during conversion.
Whether to ignore case sensitivity.
The Enum. Parse method actually returns an object reference (a string) ---- we need to convert the display of this string to the desired Enumeration type (it is a binning operation ).
Vi. namespace
1. The namespace provides a way to organize related classes and other types. Unlike files and components, a namespace is a logical combination, not a physical combination.
2. You cannot declare multiple namespaces nested in another namespace.
3. The namespace is irrelevant to the Assembly. The same dataset can have different namespaces or define the types in the same one.
4. namespace alias
If the name of a namespace is long and must be referenced multiple times in the code, but you do not want the name to be included in the using command (for example, to avoid class name conflicts), you can
This namespace specifies an alias:
Using System. Text;
Using System. Threading. Tasks;
Namespace Wrox. ProC. basic
{
Class NameSpaceEx
{
}
}
Namespace ConsoleApplication1
{
Using Intro = Wrox. ProC. basic; // specify the alias Intro for Wrox. ProC. basic.
Class Program
{
Static void Main (string [] args)
{
Intro: NameSpaceEx nex = new Intro. NameSpaceEx (); // instantiate the NameSpaceEx object. Use the: Modifier
}
}
}
VII. Main () method
C # The program runs from the Main () method. This method must be a static method of a class or structure, and its return type must be int or void.
1. Multiple Main () Methods
When compiling a C # console or a Windows application, the compiler searches for the Main () method that matches the preceding signature in the class by default, and call this class method the entry of the program.
If multiple Main () methods exist, the compiler reports an error.
Namespace Wrox
{
Class Client
{
Public static int Main ()
{
MathEx. Main ();
Return 0;
}
}
Class MathEx
{
Static int Add (int x, int y)
{
Return x + y;
}
Public static int Main ()
{
Int I = Add (5, 10 );
Return I;
}
}
}
However, you can use the/main option, followed by the full name of the class to which the Main () method belongs (including the name being null), to clearly tell the Compiler which method is used as the entry of the program:
Csc ConsoleApplication1.cs/main: Wrox. MathEx
2. Pass parameters to the Main () method
When calling a program, the CLR can contain a parameter and pass the command line parameter to the program. This parameter is a string array, traditionally called args (but C # can accept any name ).
Class Program
{
Static void Main (string [] args)
{
For (int I = 0; I <args. Length; I ++)
{
Console. WriteLine (args [I]);
}
}
}
You can use the command line to compile this code.
ConsoleApplication1 a B c
A
B
C
VIII. C # pre-processor commands
C # Preprocessor commands will not be converted into executable code commands, but will affect all aspects of the compilation process. For example, using the C # Preprocessor command can prohibit the compiler from compiling a part of the code.
If you plan to release two versions of code, that is, the basic code and the Enterprise Edition with more features, you can use the pre-processor command.
C # provides other mechanisms to implement C ++ commands, such as custom features. C # does not have an independent pre-processor like C ++. The so-called pre-processor commands are actually processed by the compiler.
In this case, C # retains some Preprocessor command names, because these commands may be considered as preprocessors.
1. # define and # undef
# Define DEBUG
It tells the compiler that there is a given name symbol, similar to declaring a variable, but there is no real value, just exist. This symbol is not part of the actual code, but is only used to compile the code in the compiler.
. It makes no sense in C # code.
# Undef DEBUG
Delete a symbol
# Define is useless. It is powerful when used with other pre-processor commands (especially # if.
2. # if, # elif (= else if), # else and # endif
These commands tell the compiler whether to compile a code block
# If DEBUG
// Do something
# Endif
# If, # elif also supports the logical operator "!", "= ","! = "," | ", And or...
3. # warning, # error
When the compiler encounters them, the text following these two commands is generated as a warning or error.
# If DEBUG
# Error "you didnot define DEBUG"
# Endif
# Warning "dont forget to remove"
4. # region and # endregion
# Region and # endregion are used to mark a piece of code as a block with a given name.
# Region Member Field
Int x;
Double d;
# Endregion
They can be recognized by some compilers.
5. # pragma
# The pragma command can suppress or restore specified compilation warnings.
# Pragma warning disable
Int I;
# Pragma warning restore
Blog may be written in the error, welcome readers to correct criticism, mailbox 15734108350@163.com.