您的位置:首页 > 其它

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

2008-05-24 13:06 134 查看
#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 语句结束后被摧毁了

}

}

}
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: