不用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 语句结束后被摧毁了
}
}
}
}
}
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 语句结束后被摧毁了
}
}
}
}
}
相关文章推荐
- 终于发现自己存在的一个问题:关于用不用VC创建对象的问题
- Java 数组中new Object[5]语句是否创建了5个对象?
- C++用new创建对象和不用new创建对象的区别解析
- 【技术贴】解决打开程序出错,提示错误429,activex部件不能创建对象,不用重装系统。
- C++创建对象时用NEW 和不用 NEW的区别和概念
- C++用new和不用new创建类对象区别
- c++中用new和不用new创建对象的本质区别
- C++用new和不用new创建类对象区别
- C++用new和不用new创建类对象区别
- 数据库基本操作(一) DDL语句:对数据库内部对象创建修改删除
- C++用new创建对象和不用new创建对象的区别解析
- C# using语句使用流还需要显示调用Close方法吗?
- oracle中的对象创建及删除语句【原创】
- 第17条:以独立语句将new创建的对象存储在智能指针中
- 查询某个对象的创建语句
- 下面这条语句一共创建了多少个对象:String s="a"+"b"+"c"+"d";
- close操作前要测试“文件对象”是否创建成功,如果多个流,需要分别去关闭
- IO流操作中创建的流对象为什么一定close?
- C++用new和不用new创建类对象区别
- SQL语句大全:侧重创建、判断对象是否存在相关语句