"C # trivia" In C # some easy to confuse concept summary (ix)---------exception, file stream object,. NET serialization, multithreading

Source: Internet
Author: User
Tags net serialization

finally have time to complete the final chapter of this series on exceptions, file stream objects, serialization, multithreading issues in C #.

First, let's look at the relevant knowledge about anomalies.

One, abnormal

First distinguish three concepts: warnings, exceptions, and errors. These three concepts should be familiar to both novice and veteran programmers, and are often encountered.

    1. warning : occurs in a program and has no effect on the execution of the program.
    2. Exception : Occurs at run time and will stop once the program has occurred.
    3. error : Always refers to a program that does not conform to the syntax specification.

The standard throw exception in C # is the following statement:

Try             {                // attempted execution of statement             }            catch  (Exception)            {                 // Throw exception statement                throw;            }             finally             {                // final executed statement            }

The most common question about try....catch statements is whether the statements in the try are returned before or after finally.

Look at the following code:

 Public intTest () {inti =1; intK =0; intm =1; Try            {                //attempted execution of the statement//int n = i/k;                returnm; }            Catch(Exception) {//the statement that throws the exception                Throw; }            finally            {                //The final statement executedM=m+2; }        }

What is the return value when we call the test () method in the main () method? The answer is: 1. So what is this for?

This is the use of our anti-compilation tool, the anti-compilation code is as follows:

From the above we can see that the return value in the try is first saved in a variable that is automatically generated by. NET Framwork and returned after finally.

The finally statement is executed at the end, and only the value that is returned in the try does not return, and the return state also occurs after finally execution.

Two, the file stream object

The file stream object refers to the FileStream object, which is primarily used for large file operations. Let's take a look at the sample code to copy a large file using the pair image

//Read the file         Public stringFileCopy () {//creates a file stream object and assigns the file stream object the path to the action file and how the file is manipulatedFileStream stream =NewFileStream (@"C:\2.txt", FileMode.Open); //prepares a byte array for the file stream object to read the data and put it inside the array//Public override int read (byte[] array, int offset, int count); The Read method of the Streamfile object is only one method            byte[] buffer=New byte[1024x768*1024x768]; //The method of invoking the file stream to read the data, placing the read bytes in the buffer arrayStream. Read (Buffer,0, buffer.            Length); //converts a byte array to a string with the specified encoding           stringStr=Encoding.Default.GetString (buffer); //Freeing ResourcesStream.            Dispose (); returnstr; }        //Write a file         Public voidFileWrite (stringstr) {FileStream Strwrite=NewFileStream (@"D:\2.txt", FileMode.Create); //converts a string to a specified encoded byte array            byte[] buffer =Encoding.UTF8.GetBytes (str); //Write ObjectStrwrite.write (Buffer,0, buffer.            Length);        Strwrite.dispose (); }

The process is as follows:

Three. NET serialization issues

Serialization is primarily to preserve the state of objects and persist them to a device (for example, a hard disk);

For example, we are now going to save the state of the object's properties and look at the code that buys you:

C # Small Knowledge C # Some easy-to-confuse concepts summary (ix)---------exceptions, file stream objects,. NET serialization, multithreading

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.