C # Exception Handling (2)

Source: Internet
Author: User
Iv. Using statements

In C #, the using statement is the closest to the "ideal" version:

Private Static char [] readsource (string filename)
{
Fileinfo file = new fileinfo (filename );
Int length = (INT) file. length;
Char [] source = new char [length];
Using (textreader reader = file. opentext ())
{
Reader. Read (source, 0, length );
}
Return source;
}

Reader will be properly disabled. To put it simply, the using statement has a large number of features that can improve the "ideal" version at the beginning. First, let's take a look at its internal operating mechanism.
Using statement Conversion
C # ECMA standard description using statement:
Using (Type Variable = initialization)
Embeddedstatement
It is equivalent
{
Type Variable = initialization;
Try
{
Embeddedstatement
}
Finally
{
If (variable! = NULL)
{
(Idisposable) variable). Dispose ();
}
}
}
It depends on the idisposable interface in the system namespace:
Namespace System
{
Public interface idisposable
{
Void dispose ();
}
}
Note: restrained conversions in finally blocks mean that this variable must be a class that supports the idisposable interface (through inheritance or conversion operations ). If it is not, you will get a compilation error.
Using textreader Conversion
Unexpectedly, textreader supports the idisposable interface and implements dispose to call and close the interface. This means:
Using (textreader reader = file. opentext ())
{
Reader. Read (source, 0, length );
}
It is equivalent to the following:
{
Textreader reader = file. opentext ();
Try
{
Reader. Read (source, 0, length );
}
Finally
{
If (reader! = NULL)
{
(Idisposable) Reader). Dispose ();
}
}
}
In addition to the forced conversion of idisposable, this is the same as the most common Java solution. This forced conversion is required because it is a common solution.

5. Processing in custom Mode
This is helpful when you consider what will happen if textreader does not implement the idisposable interface. This tutorial will show you how to implement dispose in our own classes. One way is to use the Object Adapter mode. For example:
Public sealed class autotextreader: idisposable
{
Public autotextreader (textreader target)
{
// Precondition (target! = NULL );
Adaptee = target;
}

Public textreader
{
Get {return adaptee ;}
}

Public void dispose ()
{
Adaptee. Close ();
}

Private readonly textreader adaptee;
}
You can use your own class as follows:
Using (autotextreader scoped = new autotextreader (file. opentext ()))
{
Scoped. textreader. Read (source, 0, length );
}
You can use implicit conversion operators to simplify the problem:
Public sealed class autotextreader: idisposable
{
...
Public Static Implicit operator autotextreader (textreader target)
{
Return new autotextreader (target );
}
...
}
This will allow you to use it like this:
Using (autotextreader scoped = file. opentext ())
{
Scoped. textreader. Read (source, 0, length );
}

Struct: Another option
Autotextreader intentionally uses it as a sealed class and considers its name as recommended to be used as a local variable. It is more meaningful to use a structure to replace classes:
Public struct autotextreader: idisposable
{
// Exactly as before
}
Using a structure to replace classes also provides several free optimizations. Since a structure is a value type, it can never be a null value. This means that the compiler must check the generated Finally block for null values. In addition, since you cannot inherit from a structure, its runtime will be consistent with the type during compilation. This means that the compiler generally performs forced conversion in the Finally block generated and thus avoids a packing operation (especially, if dispose is a public implicit interface implementation rather than an undisclosed display interface implementation, this will avoid forced conversion ).
In other words, it is like this:
Using (autotextreader scoped = file. opentext ())
{
Scoped. textreader. Read (source, 0, length );
}
Converted:
{
Autotextreader scoped = new file. opentext ();
Try
{
Scoped. textreader. Read (source, 0, length );
}
Finally
{
Scoped. Dispose ();
}
}
Therefore, I prefer to use the using statement instead of the finally program for processing. In fact, the using statement solution has the following advantages over the earlier "ideal" version: A using statement:
· Running, it can always release resources
· Is an extension mechanism. It allows you to create a set of resources to release. It is easy to create your own resource release class, such as autotextreader.
· Allows you to pair Resource Acquisition with resource release. The best time to release resources is when you get resources. Just like if you borrow a book from the library, you can be told when to return it .]
· The syntax structure clearly tells you that you are using a resource.
· Create a range for variables with resources. Observe the compiler conversion of the using statement carefully and you will find that it uses an external pair of parentheses intelligently.

Using (autotextreader scoped = file. opentext ())
{
Scoped. textreader. Read (source, 0, length );
}
Scoped. textreader. Close (); // scoped is not in scope here
 
This is a trace of the condition declaration in C ++. It allows you to limit the range of variables used. variables are useful only in this range. When a variable can be used, they can only exist in this range.

 

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.