C#语言中为什么在如下的语句中需要使用using?string path1 = "E:/TEMP/C#/123.txt";using (StreamReader sr = new StreamReader(path1)){String line;// Read and display lines from the file until the end of // the file is reached.while ((line

来源:学生作业帮助网 编辑:作业帮 时间:2024/05/02 20:44:48
C#语言中为什么在如下的语句中需要使用using?string path1 =

C#语言中为什么在如下的语句中需要使用using?string path1 = "E:/TEMP/C#/123.txt";using (StreamReader sr = new StreamReader(path1)){String line;// Read and display lines from the file until the end of // the file is reached.while ((line
C#语言中为什么在如下的语句中需要使用using?
string path1 = "E:/TEMP/C#/123.txt";
using (StreamReader sr = new StreamReader(path1))
{
String line;
// Read and display lines from the file until the end of
// the file is reached.
while ((line = sr.ReadLine()) = null)
{
Console.WriteLine(line);
textBox1.Text = line;
}
Sr.Close();//注意这里close必须在using程序段之内;
}

C#语言中为什么在如下的语句中需要使用using?string path1 = "E:/TEMP/C#/123.txt";using (StreamReader sr = new StreamReader(path1)){String line;// Read and display lines from the file until the end of // the file is reached.while ((line
using 语句定义一个范围,在此范围的末尾将处理对象.
using (expression | type identifier = initializer) statement
其中:
expression
希望在退出 using 语句时调用 Dispose 的表达式.
type
identifier 的类型.
identifier
type 类型的名称或标识符.定义一个以上 type 类型的 identifier 是可以的.在每一个 identifier = initializer 的前边都有一个逗号.
initializer
创建对象的表达式.
statement
嵌入的语句或要执行的语句.
备注
在 using 语句中创建一个实例,确保退出 using 语句时在对象上调用 Dispose.当到达 using 语句的末尾,或者如果在语句结束之前引发异常并且控制离开语句块,都可以退出 using 语句.
实例化的对象必须实现 System.IDisposable 接口.
示例
// cs_using_statement.cs
// compile with /reference:System.Drawing.dll
using System.Drawing;
class a
{
public static void Main()
{
using (Font MyFont = new Font("Arial",10.0f),MyFont2 = new Font("Arial",10.0f))
{
// use MyFont and MyFont2
} // compiler will call Dispose on MyFont and MyFont2
Font MyFont3 = new Font("Arial",10.0f);
using (MyFont3)
{
// use MyFont3
} // compiler will call Dispose on MyFont3
}
}

C#语言中为什么在如下的语句中需要使用using?string path1 = E:/TEMP/C#/123.txt;using (StreamReader sr = new StreamReader(path1)){String line;// Read and display lines from the file until the end of // the file is reached.while ((line 在c#语言中, c#中如何使用带过滤条件的查询语句作为数据源 在C#中,定义派生类时,指定基类应使用的语句是( ).A.Inherits B.:C.Class D.Overrides 谢 (单选题)在C#语言开发的程序中,以下关于反射的说法中错误的是().A.使用反射需要引入System.Renection命名空间B.使用反射不可以在运行时创建类型实例C.使用反射时不需要提供解决方案D.使用 判断:在switch语句中可以根据需要使用或不使用break语句. 13.下列关于switch语句和break语句的结论中,正确的是(C ).A.break语句是switch语句中的一部分 B.在switch语句中必须使用break语句C.在switch语句中可以根据需要使用或不使用break语句 D.switch语句是bre c#中怎么使用if语句(如果…就…否则…) C#中一道计算题.选哪个,为什么?在C#语言中,有如下定义,程序的执行结果是:( ) (选择1项)static void f(int x,int y){ int t;if (x 在C#中开方怎么表示 能使用sqrt么? c语言中为什么需要for循环?其本义是不是我们需要执行很多次的语句而利用for循环只要写两个语句就好了? C#语言中得到数据类型的函数是什么 为什么在c语言中有的语句后面需要加这注释符号(//) 还有在什么时候需要加这“//”注释符号?有时在需要加这符号时,这符号可以省略吗?知道的告诉我下, C# 语言中,在if 分支结构中,条件表达式的值必须是什么类型的数据? C# Goto语句如何使用 C语言中,在main函数一开始定义了 int i;后来又在for语句中有int i 这样的语句,为什么编译时出错. 如何在C语言中使用汉字作为if的判断语句? C语言中怎样使用goto语句最好是带有例子的解释