9.3.2 clean up resources with IDisposable interface

Source: Internet
Author: User
Tags finally block

9.3.2 clean up resources with IDisposable interface

We have used several types that implement the IDisposable interface, such as Graphics and SolidBrush. We want to make the code as easy to understand as possible, so we explicitly call the Dispose method when we're done with the object.

C # contains syntactic sugars, which, in the form of using statements, ensure that even if an exception is thrown in the body of a statement, the dispose;f# can be called with a similar structure, using the USE keyword. Listing 9.11 is a simple example of working with a file.

Listing 9.11 using the USE keyword to process a file (f#interactive)

> Open System.IO;;

> Let readFile () =

Use reader = Newstreamreader ("C:\\Test.txt") [1]

Let text =reader. ReadToEnd ()

Console.Write (text)

;; [2]

Val readfile:unit–> Unit

> ReadFile (); | Output the contents of the sample file

Hello world! |

Ahoj svete! |

Create StreamReader (implements the IDisposable interface), we use the Using keyword Declaration [1]. Note that this syntax is similar to the LET keyword that is normally used in let, and the F # compiler automatically adds a call to the Dispose method at the end of the function [2], so that it is automatically freed after we have finished processing StreamReader. The compiler also inserts a try-finally block to ensure that it can be cleaned even when an exception occurs.

The important difference between the using structure in C # and the use keyword in F # is that in C #, you must explicitly use braces to specify the using scope, whereas in F #, the Dispose method is called when the value is visible at the end of the scope, usually this is what we need, so write many generations The code becomes very easy. Listing 9.12 shows the versions of C # and F #.

Listing 9.12 cleaning up resources in F # and C #

F # version

Let test () =

Use reader = Newstreamreader ("C:\\Test.txt")

Let text = reader. ReadToEnd ()

Console.Write (text) [1]

C # version

void Test () {

using (var reader = Newstreamreader ("C:\\Test.txt")) {

var text =reader. ReadToEnd ();

Console.Write (text);

} [2]

}

In both languages, objects are freed when they run to a scope that leaves the reader value accessible. In F #, the default occurs at the end of a function, which is usually what we need. When a function's code needs to run continuously for a long time, it better ensures that the resource is released as soon as possible. Let me say that you want to close the file and then output the content to the console after it is closed. In C #, we have to create a local variable inside the function and assign it to it within the using block, which is easier to do in F # because we can use indentation to specify the scope:

Let test () =

Let text =

Use reader = Newstreamreader ("C:\\Test.txt")

Reader. ReadToEnd ()

Console.Write (text)

This syntax may be a little surprising, but once we understand that in F #, every block of code is an expression, it becomes clear. In the preceding code, we specify the way to construct this expression, which is the same as we write (1+2), not the default 1+ (2*3). In this way, we can restrict the scope of the value reader to the expression that initializes the value text.

Although the Use keyword is primarily used to handle some resource. NET objects, it uses a wide range of applications. Let's take a look at an example.

9.3.2 clean up resources with IDisposable interface

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.