Introduction to using statements in C # Language

Source: Internet
Author: User
Background

External sorting refers to the sorting of large files, that is, the records to be sorted are stored in external storage, and files to be sorted cannot be loaded into memory at a time, data exchange between memory and external memory is required multiple times to sort the entire file. The most common algorithm for external sorting is multi-channel Merge Sorting, which breaks down the original file into multiple parts that can be loaded into memory at one time and transfers each part to the memory for sorting. Then, merge and sort the sorted sub-files.

Problem proposal

Suppose we want to write an external sorting program. Now we will discuss how to merge and sort sorted sub-files.

Solution 1

The following is the code snippet of the external sorting and merging phase:

01: Class externalsorting02: {03: void Merge (string inputfilename1, string inputfilename2, string outputfilename) 04: {05: Using (VAR reader1 = new streamreader (inputfilename1) 06: {07: Using (VAR reader2 = new streamreader (inputfilename2) 08: {09: Using (VAR writer = new streamwriter (outputfilename) 10: {11: Merge (reader1, reader2, writer); 12:} 13:} 14:} 15:} 16: 17: void Merge (textreader reader1, Textreader reader2, textwriter writer) 18: {19: var S1 = reader1.readline (); 20: var S2 = reader2.readline (); 21: While (S1! = NULL | S2! = NULL) 22: {23: If (compare (S1, S2) <= 0) stepit (ref S1, reader1, writer); 24: else stepit (ref S2, reader2, writer); 25 :}26 :}27: 28: int compare (string S1, string S2) 29: {30: If (S1 = NULL & S2 = NULL) throw new argumentexception ("S1 and S2 cannot be both null"); 31: If (S1 = NULL) return 1; 32: If (s2 = NULL) Return-1; 33: Return string. compare (S1, S2); 34:} 35: 36: void stepit (ref string S, textreader reader, textwriter writer) 37: {38: writer. writeline (s); 39: S = reader. readline (); 40:} 41 :}

In the above Code, the three using statements from lines 05th to 14 are nested one by one and indented one by one. Isn't it ugly?

Note: In the above Code, the 33rd lines can replace the method you want to compare the size of the Code to sort by different keywords.

Solution 2

We know that multiple objects can be used with the using statement, but these objects must be declared in the using statement. Therefore, we can refactor the above 05th to 14 lines of code as follows:

1:  using (TextReader reader1 = new StreamReader(inputFileName1),2:                    reader2 = new StreamReader(inputFileName2))3:  {4:    using (TextWriter writer = new StreamWriter(outputFileName))5:    {6:      Merge(reader1, reader2, writer);7:    }8:  }

However, there are two nested using statements, which are not satisfactory.

Solution 3

We also know that the C # compiler actually converts the using statement into a try-Finally block. Then we continue to refactor:

01:  TextReader reader1 = null;02:  TextReader reader2 = null;03:  TextWriter writer = null;04:  try05:  {06:    reader1 = new StreamReader(inputFileName1);07:    reader2 = new StreamReader(inputFileName2);08:    writer = new StreamWriter(outputFileName);09:    Merge(reader1, reader2, writer);10:  }11:  finally12:  {13:    if (reader1 != null) reader1.Dispose();14:    if (reader2 != null) reader2.Dispose();15:    if (writer != null) writer.Dispose();16:  }

This looks good. Note:

  • If the preceding code snippet is not the only code block in the merge method, enclose it with a pair of braces to create a limited range for the preceding three objects (reader1, reader2, and writer.
  • In fact, the C # compiler generates similar il code for solution 1 and solution 2, all of which are three nested try-finally blocks. Instead of having only one try-Finally block as in solution 3.
Solution 4

We know that the using statement only provides a convenient syntax that ensures correct use of the idisposable object. The Using statement calls the dispose method on the object in the correct way, and causes the object to be out of the scope when the dispose is called. Therefore, you can refactor it as follows:

1:  using (IDisposable reader1 = new StreamReader(inputFileName1),2:                     reader2 = new StreamReader(inputFileName2),3:                     writer = new StreamWriter(outputFileName))4:  {5:    Merge(reader1 as TextReader, reader2 as TextReader, writer as TextWriter);6:  }

This is my favorite solution. What about you?

Solution 5

However, in solution 4, it is uncomfortable to use three as to force type conversion in row 5th. Consider the following refactoring:

1:  using (TextReader reader1 = new StreamReader(inputFileName1),2:         TextReader reader2 = new StreamReader(inputFileName2),3:         TextWriter writer = new StreamWriter(outputFileName))4:  {5:    Merge(reader1, reader2, writer);6:  }

Unfortunately, the above code snippets cannot be compiled and the following compilation error occurs:

Cs1044: Multiple types cannot be used in for, using, fixed, or declaration statements.

This compilation error is unreasonable. In fact, it is useful for programmers to use multiple types in the for, using, and fixed statements, and it is not difficult for the C # compiler to implement this. What do you think?

Solution 6

Okay, let's use another method for refactoring:

1:  using (var reader1 = new StreamReader(inputFileName1),2:             reader2 = new StreamReader(inputFileName2),3:             writer = new StreamWriter(outputFileName))4:  {5:    Merge(reader1, reader2, writer);6:  }

Unfortunately, compilation fails. The error message is as follows:

Cs0819: A local variable of the implicit type cannot have multiple declarations.

This compilation error is unreasonable. Likewise, it is useful for programmers to have multiple declarations in implicit local variables, and there is no implementation difficulty for the C # compiler. What do you think?

Conclusion

We recommend that you modify the C # compiler and remove the cs1044 and cs0819 compilation errors to allow solution 5 and Solution 6 for the benefit of C # programmers.

For the current C # compiler, solution 4 is recommended.

 

References
  1. Using Statement (C # reference)
  2. Wikipedia: External sotring

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.