Using Keyword usage and Function

Source: Internet
Author: User
In fact,. net learners have been familiar with the using keyword at the beginning and may not be very concerned about it, including me. Until today, when someone asked me about the role of using, it caught my attention.
In general, there are two types: first, using is used as a reference to the namespace (this is the call.. This is mainly the second type. Let's take a look at a small demo!

Code
1 using system;
2 using system. IO;
3
4/** // <summary>
5 // Summary of filedemo
6 /// </Summary>
7. Public class filedemo
8 {
9 Public filedemo ()
10 {
11 //
12 // todo: add the constructor logic here
13 //
14}
15 public void mainmethodone () // This is using usage, which implicitly calls the dispose () method
16 {
17 // create a string to write a temporary file
18 byte [] bytestowrite = new byte [] {1, 2, 3, 4, 5 };
19 // create a temporary file
20 using (filestream FS = new filestream ("temp.txt", filemode. Create ))
21 {
22 // write the string to a temporary file
23 fs. Write (bytestowrite, 0, bytestowrite. Length );
24}
25 // Delete temporary files
26 file. Delete ("temp.txt"); // currently, it can always release resources.
27}
28
29 public void mainmethodtwo () // This is the display that calls the dispose () method
30 {
31 // create a string to write a temporary file
32 byte [] bytestowrite = new byte [] {1, 2, 3, 4, 5 };
33 // create a temporary file
34 filestream FS = NULL;
35 try
36 {
37 fs = new filestream ("temp.txt", filemode. Create ));
38 // write a string to a temporary file
39 FS. Write (bytestowrite, 0, bytestowrite. Length );
40}
41 finally
42 {
43 // close the file explicitly after writing
44 If (FS! = NULL)
45 {
46 (idisposable) fs. Dispose ();
47}
48}
49 // Delete temporary files
50 file. Delete ("temp.txt ");
51}
52}
53

Here, using serves as the survival region of a temporary object. Because. Net is a mechanism for hosting code, we sometimes need code that requires unmanaged resources, such as file handles or SQL connections. After you use one or more of these resources to complete the code, the using block ensures that these resources are released. In this case, we need to use using.
The using block can be divided into three parts: acquisition, use, and release.
1. Obtain and initialize a variable to reference system resources. In using ()
2. access resources and perform operations using resources. The statement between using {} represents the resource usage process.
3. Release indicates that the dispose method is called for objects in resourcename. This allows the object to completely terminate its unmanaged resource "}" block to release the resource controlled by the "{" block.
Basically, that's all!

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.