This tutorial references to the C # and the ASP.NET program design tutorial, if there is any deficiencies, please point out, or
The ideal blog of the old cat.
I don't write some days. There is less today, and several netizens always ask, I wrote something. It may be more missed, I hope everyone can help
Pretreatment instructions: Unlike C , C # has no independent preprocessor. In C #, the pre-processing instruction is not a separate processing step before the compiler begins to compile the code, but is performed as part of the lexical analysis. The pretreatment instruction starts with ## and is located in the lead.
#define instructions are used to define the match, his scope is the entire file where the definition is located, and the symbol definition must be placed in front of all other statements, or before all "real code". (For example: "Using System" is an actual code.)
If you want to cancel a certain symbol, you want to use the #undef instruction.
There are 4: # IF, # Elif, # else, # ENDIF, they are used to include some program code into in or exclude. Conditional compile instructions and IF statements have a similar role. You can also use logic and (&&), logical, or (||), equal to (==), or equal to (! =), Etc. in condition compilation instructions.
EG:
#define mf1
#define mf2
Using system;
Public Class Mikecat
{
Public static void main ()
{
#if (MF1 &&! MF2)
Console.WriteLine ("MF1 is defined");
#elif (! MF1 && MF2)
Console.WriteLine ("MF2 is defined");
#elif (MF1 && MF2)
Console.writeline ("MF1 and MF2 is defined);
#ELSE
Console.WriteLine ("MF1 and MF2 are not defined");
#ENDIF
}
} // Operation results: MF1 and MF2 are defined
#Error and #warning instructions are used to issue compilation errors and warnings.
EG:
#define mf1
#define mf2
Using system;
Public Class Mikecat
{
Public static void main ()
{
#if mf1
#Warning Welcomes the ideals to the old cat!
#ENDIF
#if mf2
#error's ideal blog error
#ENDIF
}
} // Operation: Test.cs (9,17): Warning CS1030: #warning: "Welcome to the old Cat!"
//test.cs (12, 15): Error CS1029: #error: "The ideal blog of the old cat"
#LINE instruction is used to modify the compiler line number and file name
EG:
Using system;
Public Class Mikecat
{
Public static void main ()
{
#LINE 66 "mfblog.cs" // set the compiling line number to 66 and rename the file name to mfblog.cs
INTT I = 1;
Console.WriteLine ("The value of i is {0}", i);
}
} // Operation results: mfblog.cs (66, 6): Error CS0246: Can't find the type or namespace name "INTT" (whether it is missing the USING instruction or assembly reference?)
//mfblog.cs (67, 34): Error CS0103: Name "i" does not have exception handling in class or namespace "Mikecat": in C #, there are two status of abnormalities, first: in the program Using the throw statement, there is an unclear unclear. The second case is that the C # statement or expression inspired a certain exception during the execution, so that the operation cannot end normally, thereby causing an exception.
In C #, the exception is processed by the TRY statement. The TRY statement provides a mechanism to capture an exception thrown during the program. TRY has three possible structures, namely: try-catch | try-finally | TRY-CATCH-FINALLY
Try-catch structure: Try clause follows one or more CATCH clauses. If an exception is thrown when the statement in the TRY clause is executed, the program will find the first Catch clause that can handle the exception, and transfer control to the CATCH clause. Nothing is not defined, and the CATCH clause of the abnormal variable is not defined as a normal CATCH clause. A TRY clause can only have one normal CATCH clause, and the clause must be ranked behind other CatCH clauses.
EG:
Using system;
Class Mikecat
{
Static void MF1 (String S)
{
IF (s == NULL)
Throw (new argumentnullexception ()); // triggered an exception
}
Static void MF2 ()
{
Try
{
String s = null;
Mf1 (s); // Call the MF () method. Because s = null, it will trigger an exception
}
Catch (argumentnullexception ex)
{
Console.writeLine ("" Eximdened: {0} ", EX.MESSAGE);
Throw; // once again
}
}
Public static void main ()
{
Try
{
MF2 (); // Call MF2 () method
}
Catch (argumentnullexception ex)
{
Console.WriteLine ("" MAIN () method in the main () method; E.MESSAGE);
}
}
} // mf2 () method in the method: value cannot be empty.
The abnormal value in the // main () method cannot be empty.
Try-finally structure: Try clause follows one Finally clause. Regardless of how the try clause exits (whether normal exit or exception, the GOTO | BREAK | Continue | Return statement exits), the program's control will always be transferred to the finally clause execution.
EG:
Using system;
Public Class Mikecat
{
Public static void main ()
{
Try
{
Console.writeLine ("Execute TRY Sentence";
// goto Leave; // Jump to Leave Tag
Return;
}
Finally
{
Console.WriteLine ("Execute Finally Subs";
}
Leave:
Console.writeline ("Execute Leave Tags!");
}
} // Execute the TRY clause to execute the finally clause
Try-catch-finally: Try clause follows one or more catch clauses and a finally clause
EG:
Using system;
Class Mikecat
{
Static void MF (String S)
{
IF (s == NULL)
Throw (new argumentnullexception ()); // triggered anomalies}
Public static void main ()
{
Try
{
String s = null;
Mf (s); // Call the MF () method, due to S = NULL, it will trigger an exception
}
Catch (argumentnullexception ex)
{
Console.writeline ("Exception: {0}", EXMESSAGE);
}
Finally
{
Console.WriteLine ("Execute Finally Subs";
}
}
}