zoukankan      html  css  js  c++  java
  • using的用法

    第一种用法:引用命名空间  using System.Data.SqlClient;

    第二种用法: using(...){  }

    using (FileStream fs = File.Open("", FileMode.Open, FileAccess.Read))
    {
         using (StreamWriter sw = new StreamWriter(fs, Encoding.GetEncoding("gb2312")))
          {
              sw.WriteLine("要写入的一行字符串".Replace("
    ", "
    "));
          }
    }
    FileStream fs = File.Open("", FileMode.Open, FileAccess.Read);
    StreamWriter sw = new StreamWriter(fs, Encoding.GetEncoding("gb2312"));
    sw.WriteLine("要写入的一行字符串".Replace("
    ", "
    "));
    sw.Close();
    fs.Close();

    以上两种写法效果是一样的,只不过用了using后,那么该默认实现了IDisposable接口的对象会自动调用Dispose()方法,即自动释放资源,进而达到优化内存的效果。

     而不写using,则需要主动关闭这两个对象来释放资源。
    在程序编译阶段,编译器会自动将using语句生成为try-finally语句,并在finally块中调用对象的Dispose方法,来清理资源。所以,using语句等效于try-finally语句

    --------------------- 

    来源:CSDN
    原文:https://blog.csdn.net/weixin_38286173/article/details/80458527 

  • 相关阅读:
    HDU 1496 Equations
    HDU 1060 Leftmost Digit
    HDU 1391 Number Steps
    HDU 1212 Big Number
    HDU 1491 Octorber 21st
    HDU 1339 A Simple Task
    HDU 2710 Max Factor
    HDU 1176 免费馅饼
    FORTH基本堆栈操作
    FORTH 安装使用
  • 原文地址:https://www.cnblogs.com/fu609277362/p/9835607.html
Copyright © 2011-2022 走看看