“使用"语句与“最终尝试" [英] 'using' statement vs 'try finally'

查看:21
本文介绍了“使用"语句与“最终尝试"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一堆要使用读/写锁的属性.我可以使用 try finallyusing 子句来实现它们.

I've got a bunch of properties which I am going to use read/write locks on. I can implement them either with a try finally or a using clause.

try finally 中,我会在try 之前获取锁,并在finally 中释放.在 using 子句中,我将创建一个类,该类在其构造函数中获取锁,并在其 Dispose 方法中释放.

In the try finally I would acquire the lock before the try, and release in the finally. In the using clause, I would create a class which acquires the lock in its constructor, and releases in its Dispose method.

我在很多地方都使用了读/写锁,所以我一直在寻找比try finally更简洁的方法.我很想听听一些关于为什么不推荐一种方式或为什么一种方式可能比另一种方式更好的想法.

I'm using read/write locks in a lot of places, so I've been looking for ways that might be more concise than try finally. I'm interested in hearing some ideas on why one way may not be recommended, or why one might be better than another.

方法一(try finally):

static ReaderWriterLock rwlMyLock_m  = new ReaderWriterLock();
private DateTime dtMyDateTime_m
public DateTime MyDateTime
{
    get
    {
        rwlMyLock_m .AcquireReaderLock(0);
        try
        {
            return dtMyDateTime_m
        }
        finally
        {
            rwlMyLock_m .ReleaseReaderLock();
        }
    }
    set
    {
        rwlMyLock_m .AcquireWriterLock(0);
        try
        {
            dtMyDateTime_m = value;
        }
        finally
        {
            rwlMyLock_m .ReleaseWriterLock();
        }
    }
}

方法二:

static ReaderWriterLock rwlMyLock_m  = new ReaderWriterLock();
private DateTime dtMyDateTime_m
public DateTime MyDateTime
{
    get
    {
        using (new ReadLock(rwlMyLock_m))
        {
            return dtMyDateTime_m;
        }
    }
    set
    {
        using (new WriteLock(rwlMyLock_m))
        {
            dtMyDateTime_m = value;
        }
    }
}

public class ReadLock : IDisposable
{
    private ReaderWriterLock rwl;
    public ReadLock(ReaderWriterLock rwl)
    {
        this.rwl = rwl;
        rwl.AcquireReaderLock(0);
    }

    public void Dispose()
    {
        rwl.ReleaseReaderLock();
    }
}

public class WriteLock : IDisposable
{
    private ReaderWriterLock rwl;
    public WriteLock(ReaderWriterLock rwl)
    {
        this.rwl = rwl;
        rwl.AcquireWriterLock(0);
    }

    public void Dispose()
    {
        rwl.ReleaseWriterLock();
    }
}

推荐答案

来自 MSDN,using 语句(C# 参考)

using 语句确保即使在调用对象方法时发生异常,也会调用 Dispose.您可以通过将对象放在 try 块中然后在 finally 块中调用 Dispose 来获得相同的结果;事实上,这就是编译器如何翻译 using 语句.前面的代码示例在编译时扩展为以下代码(注意额外的大括号以创建对象的有限范围):

The using statement ensures that Dispose is called even if an exception occurs while you are calling methods on the object. You can achieve the same result by putting the object inside a try block and then calling Dispose in a finally block; in fact, this is how the using statement is translated by the compiler. The code example earlier expands to the following code at compile time (note the extra curly braces to create the limited scope for the object):

{
  Font font1 = new Font("Arial", 10.0f);
  try
  {
    byte charset = font1.GdiCharSet;
  }
  finally
  {
    if (font1 != null)
      ((IDisposable)font1).Dispose();
  }
}

所以基本上,它是相同的代码,但有一个很好的自动空检查和一个额外的变量范围.该文档还指出,它确保正确使用 IDisposable 对象",因此您可能会在未来的任何模糊情况下获得更好的框架支持.

So basically, it is the same code but with a nice automatic null-checks and an extra scope for your variable. The documentation also states that it "ensures the correct use of IDisposable object" so you might as well gets even better framework support for any obscure cases in the future.

所以选择选项 2.

将变量放在一个范围内,在它不再需要后立即结束也是一个加分项.

Having the variable inside a scope that ends immediately after it's no longer needed is also a plus.

这篇关于“使用"语句与“最终尝试"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆