using (StreamReader sr = new StreamReader("TestFile.txt")) 
            {
                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);
                }
            }
using 这句话是什么意思,在这里用using有什么作用?

解决方案 »

  1.   

    提供能确保正确使用 IDisposable 对象的方便语法。
      

  2.   

    简单的说就是在 using } 之前 会释放 ()内变量的资源
      

  3.   

    没什么意思,,,引用!
    你可去掉  StreamReader sr = new StreamReader("TestFile.txt")                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);
                    }
     
      

  4.   

    using (StreamReader sr = new StreamReader("TestFile.txt")) 
    {***}相当于
    try
    {StreamReader sr = new StreamReader("TestFile.txt");***}
    finally
    {sr.disopose();}保证在任何情况下,sr都能被自动释放~
      

  5.   

    顶  确保正确使用 IDisposable 对象的方便语法。
      

  6.   

    using 语句确保调用 Dispose,即使在调用对象上的方法时发生异常也是如此。通过将对象放入 try 块中,并在调用 finally 块中的 Dispose,可以获得相同的结果;实际上,这就是编译器转换 using 语句的方式。LZ 的代码示例在编译时将扩展到以下代码(请注意,使用额外的大括号为对象创建有限范围):{
      StreamReader sr = new StreamReader("TestFile.txt");
      try
      {
        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);
        }
      }
      finally
      {
        if (sr != null) ((IDisposable)sr).Dispose();
      }
    }
      

  7.   

    using 使用
    using(){}在
    {}中使用()中的东西,{}结束后销毁。
      

  8.   

    using 可以自动释放托管资源以及实现了 IDisposable 接口的资源!