明天吧 發表於 2020-2-19 08:23:00

C#基础:using语句的用法

<h1 style="background: rgba(43, 102, 0, 1); border-radius: 6px; box-shadow: 0 0 1px rgba(95, 90, 75, 1), 1px 1px 6px 1px rgba(10, 10, 0, 0.5); color: rgba(255, 255, 255, 1)">一、using语句</h1>
<p>using可以算是.NET中新的语法元素,它清楚地说明一个通常比较占用资源的对象何时开始使用和何时被手动释放。当using可以被使用时,建议尽量使用using语句。至今为止,使用using语句发现它带给程序员的只有优点,而没有任何弊端。</p>
<p>在.NET的环境中,托管的资源都将由.NET的垃圾回收机制来释放,而一些非托管的资源则需要程序员手动地将它们释放。.NET提供了主动和被动两种释放非托管资源的方式,即IDisposable接口的Dispose方法和类型自己的Finalize方法。任何带有非托管资源的类型,都有必要实现IDisposable的Dispose方法,并且在使用完这些类型后需要手动地调用对象的Dispose方法来释放对象中的非托管资源。</p>
<blockquote>
<p>如果类型正确地实现了Finalize方法,那么即使不调用Dispose方法,非托管资源也最终会被释放,但那时资源已经被很长时间无畏地占据了。</p>
</blockquote>
<p>using语句的作用就是提供了一个高效的调用对象Dispose方法的方式。对于任何IDisposable接口的类型,都可以使用using语句,而对于那些没有实现IDisposable接口的类型,使用using语句会导致一个编译错误。</p>
<p>先来看一个using语句的基本语法:</p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 0, 255, 1)">using</span>(StreamWriter sw= <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> StreamWriter())
{
    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 中间处理逻辑</span>
}</pre>
</div>
<p>在上面代码中,using语句一开始定义了一个StreamWriter的对象,之后在整个语句块中都可以使用sw,在using语句块结束的时候,sw的Dispose方法将会被自动调用。using语句不仅免除了程序员输入Dispose调用的代码,它还提供了机制保证Dispose方法被调用,无论using语句块顺利执行结束,还是抛出了一个异常。下面的代码演示了using的这一保护机制。</p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 0, 255, 1)">using</span><span style="color: rgba(0, 0, 0, 1)"> System;

</span><span style="color: rgba(0, 0, 255, 1)">namespace</span><span style="color: rgba(0, 0, 0, 1)"> usingDemo
{
    </span><span style="color: rgba(0, 0, 255, 1)">class</span><span style="color: rgba(0, 0, 0, 1)"> Program
    {
      </span><span style="color: rgba(0, 0, 255, 1)">static</span> <span style="color: rgba(0, 0, 255, 1)">void</span> Main(<span style="color: rgba(0, 0, 255, 1)">string</span><span style="color: rgba(0, 0, 0, 1)">[] args)
      {
            </span><span style="color: rgba(0, 0, 255, 1)">try</span><span style="color: rgba(0, 0, 0, 1)">
            {
                </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 使用using</span>
                <span style="color: rgba(0, 0, 255, 1)">using</span> (MyDispose md = <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> MyDispose())
                {
                  md.DoWork();
                  </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 抛出一个异常来测试using</span>
                  <span style="color: rgba(0, 0, 255, 1)">throw</span> <span style="color: rgba(0, 0, 255, 1)">new</span> Exception(<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">抛出一个异常</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">);
                }
            }
            </span><span style="color: rgba(0, 0, 255, 1)">catch</span><span style="color: rgba(0, 0, 0, 1)">
            {

            }
            </span><span style="color: rgba(0, 0, 255, 1)">finally</span><span style="color: rgba(0, 0, 0, 1)">
            {
                Console.ReadKey();
            }
      }
    }

    </span><span style="color: rgba(128, 128, 128, 1)">///</span> <span style="color: rgba(128, 128, 128, 1)">&lt;summary&gt;</span>
    <span style="color: rgba(128, 128, 128, 1)">///</span><span style="color: rgba(0, 128, 0, 1)"> 继承自IDisposable接口,仅仅用来做测试,不使用任何非托管资源
    </span><span style="color: rgba(128, 128, 128, 1)">///</span> <span style="color: rgba(128, 128, 128, 1)">&lt;/summary&gt;</span>
    <span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">class</span><span style="color: rgba(0, 0, 0, 1)"> MyDispose : IDisposable
    {
      </span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">void</span><span style="color: rgba(0, 0, 0, 1)"> Dispose()
      {
            Console.WriteLine(</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">Dispose方法被调用</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">);
      }
      </span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">void</span><span style="color: rgba(0, 0, 0, 1)"> DoWork()
      {
            Console.WriteLine(</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">做了很多工作</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">);
      }
    }
}</span></pre>
</div>
<p>在上面的代码中,using语句块抛出了一个异常,而该异常知道using语句结束后才被捕获。由于有了using语句的存在,即使异常被抛出,MyDispose的对象md的DIspose方法仍然被调用。 。程序输出结果如下:</p>
<p style="text-align: center"><img src="https://img2018.cnblogs.com/i-beta/1033738/202002/1033738-20200219080853639-1818416350.png"></p>
<p>事实上,C#编译器为using语句自动添加了try/finally块,所以Dispose方法能够保证被调用到,所以如下两段代码经过编译后内容将完全一致:</p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 0, 255, 1)">using</span> (MyDispose md = <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> MyDispose())
{
      md.DoWork();
}</span></pre>
</div>
<p>和</p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 0, 0, 1)">MyDispose md;
</span><span style="color: rgba(0, 0, 255, 1)">try</span><span style="color: rgba(0, 0, 0, 1)">
{
    md </span>= <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> MyDispose();
    md.DoWork();
}
</span><span style="color: rgba(0, 0, 255, 1)">finally</span><span style="color: rgba(0, 0, 0, 1)">
{
    md.Dispose();
}</span></pre>
</div>
<p>在彻底了解了using的实现原理以后,还应该注意一点使用using时常犯的错误,那就是千万不要试图在using语句块外初始化对象 ,如下面代码所示:</p>
<div class="cnblogs_code">
<pre>MyDispose md = <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> MyDispose();
</span><span style="color: rgba(0, 0, 255, 1)">using</span><span style="color: rgba(0, 0, 0, 1)"> (md)
{
    md.DoWork();
}</span></pre>
</div>
<p>看上去似乎没有任何问题,但是在多线程的程序中,上述代码就会有隐患。试想当md被初始化后程序突然产生一个异常而中断,那md对象中的非托管资源将没有机会得到释放,这对于系统来说危害是相当大的。所以在任何时候都应该在using语句中初始化需要使用的对象。</p>
<h1 style="background: rgba(43, 102, 0, 1); border-radius: 6px; box-shadow: 0 0 1px rgba(95, 90, 75, 1), 1px 1px 6px 1px rgba(10, 10, 0, 0.5); color: rgba(255, 255, 255, 1)">二、总结</h1>
<p>using语句为实现了IDisposable的类型对象调用Dispose方法,using语句能够保证使用的对象的Dispose方法在using语句块结束时被调用,无论是否有异常被抛出。C#编译器在编译时自动为using语句加上try/finally块,所以using的本质和异常捕获语句一样,但是语法更为简洁。所有using使用的对象都应该在using语句开始后再初始化,以保证所有的对象都能够被Dispose。</p><br><br>
来源:https://www.cnblogs.com/dotnet261010/p/12329706.html
頁: [1]
查看完整版本: C#基础:using语句的用法