CSHARP + Asp.net series tutorial (6)

Source: Internet
Author: User
For more information, see C # and ASP. NET. Program Design tutorial writing, please point out any shortcomings, or leave a message on the old cat's ideal blog.

I will not write it for many days. Today, there are few things. A few netizens always ask and write something again. There may be many omissions. I hope you can correct them.

Pre-processing commands: Unlike C ++, C # does not have an independent pre-processor. In C #, preprocessing commands are not compiled by the compiler. Code A separate processing step is executed as part of lexical analysis. All preprocessing commands start with # and are at the beginning of the line.

# The define command is used to define the compliance. Its scope is the entire file where the definition is located. The symbolic definition must be placed before all other statements, or in all "real code) before. (For example, "using System" is the real code .)

To cancel the definition of a symbol, the # UNDEF command is used.

There are four Conditional compilation commands: # If, # Elif, # else, And # endif. They are used to include or exclude some program code conditionally. The Conditional compilation command is similar to the IF statement. You can also use logic and (&), logic or (|), equal to (=), not equal (! =.

Eg:

# Define mf1

# Define mf2

Using system;

Public class mikecat

{

Public static void main ()

{

# If (mf1 &&! Mf2)

Console. writeline ("mf1 defined ");

# Elif (! Mf1 & mf2)

Console. writeline ("mf2 defined ");

# Elif (mf1 & mf2)

Console. writeline ("mf1 and mf2 are defined ");

# Else

Console. writeline ("mf1 and mf2 are not defined ");

# Endif

}

} // Running result: mf1 and mf2 are defined

# Error and # warning commands are used to issue compilation errors and warnings.

Eg:

# Define mf1

# Define mf2

Using system;

Public class mikecat

{

Public static void main ()

{

# If mf1

# Welcome to old cats!

# Endif

# If mf2

# Error the old cat's ideal blog has an error

# Endif

}

} // Running result: Test. CS (9, 17): Warning cs1030: # warning: "Welcome to the old cat !"

// Test. CS (): Error cs1029: # error: "The old cat's ideal blog has an error"

# The line command 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 compilation line number to 66 and rename the file name to mfblog. CS

Intt I = 1;

Console. writeline ("The I value is {0}", I );

}

} // Running result: mfblog. CS (): Error cs0246: the type or namespace name "intt" is not found (is the using command or assembly reference missing ?)

// Mfblog. CS (67,34): Error cs0103: the name "I" does not exist in the class or namespace "mikecat"



Exception Handling: in C #, two exceptions are thrown. The first is to use the throw statement in the program to immediately raise an exception unconditionally. The second case is that the C # statement or expression triggers an exception condition during execution, so that the operation cannot end normally and an exception is thrown.

In C #, exceptions are handled by try statements. The try statement provides a mechanism to capture exceptions thrown during a program. Try has three possible structures: Try-catch | try-finally | try-catch-finally

Try-catch Structure: The try clause is followed by one or more catch clauses. If an exception is thrown when a try clause is executed, the program searches for the first catch clause that can handle the exception in sequence and transfers the control to the catch clause for execution. Neither the exception type nor the catch clause defining the exception variable is called a common catch clause. A try clause can have only one common catch clause at most, and the clause must be placed behind other catch clauses.

Eg:

Using system;

Class mikecat

{

Static void mf1 (string S)

{

If (S = NULL)

Throw (New argumentnullexception (); // raises an exception

}

Static void mf2 ()

{

Try

{

String S = NULL;

Mf1 (s); // call the MF () method. An exception is thrown because S = NULL.

}

Catch (argumentnullexception ex)

{

Console. writeline ("mf2 () method exception: {0}", Ex. Message );

Throw; // triggered again

}

}

Public static void main ()

{

Try

{

Mf2 (); // call the mf2 () method

}

Catch (argumentnullexception ex)

{

Exception in console. writeline ("Main () method {0}", E. Message );

}

}

} // Mf2 () method exception: the value cannot be blank.

// The outlier value in the main () method cannot be blank.

Try-Finally structure: the try clause is followed by a finally clause. No matter how the try clause exits (whether it Exits normally or raises exceptions, or even executes the Goto | break | continue | return statement to exit), the control of the program is always transferred to the finally clause for execution.

Eg:

Using system;

Public class mikecat

{

Public static void main ()

{

Try

{

Console. writeline ("execute try clause ");

// Goto leave; // jump to the leave tag

Return;

}

Finally

{

Console. writeline ("execute finally clause ");

}

Leave:

Console. writeline ("Run the leave tag! ");

}

} // Execute the try clause to execute the finally Clause

Try-catch-finally: The try clause is followed by one or more catch clauses and one finally clause.

Eg:

Using system;

Class mikecat

{

Static void MF (string S)

{

If (S = NULL)

Throw (New argumentnullexception (); // raises an exception

}

Public static void main ()

{

Try

{

String S = NULL;

Mf (s); // call the MF () method. An exception is thrown because S = NULL.

}

Catch (argumentnullexception ex)

{

Console. writeline ("exception occurred: {0}", Ex. Message );

}

Finally

{

Console. writeline ("execute finally clause ");

}

}

}

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.