不用close了 创建的对象在using 语句结束后被摧毁了

来源:互联网 发布:淘宝客服的薪酬方案 编辑:程序博客网 时间:2024/05/01 03:22

#region Using directives

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.IO;
using System.Drawing;
using System.Windows.Forms;

#endregion

namespace UsingStatement
{
    partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void openFile_Click(object sender, System.EventArgs e)
        {
            openFileDialog.ShowDialog();
        }

        private void openFileDialog_FileOk(object sender, System.ComponentModel.CancelEventArgs e)
        {
            string fullPathname = openFileDialog.FileName;
            FileInfo src = new FileInfo(fullPathname);
            filename.Text = src.Name;
            source.Text = "";

            TextReader reader = new StreamReader(fullPathname);

/*                 如果用using 语句的话

using(TextReader reader = new StreamReader(fullPathname);  )

{

    string line;
            while ((line = reader.ReadLine()) != null)
            {
                source.Text += line + "/n";
            }
不用close了 创建的对象在using 语句结束后被摧毁了

}

         }

        }
    }
}

原创粉丝点击