Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

'using' statement vs 'try finally'

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.

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.

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.

Method 1 (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();         }     } } 

Method 2:

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();     } } 
like image 764
Jeremy Avatar asked Nov 10 '08 19:11

Jeremy


People also ask

Why do we use try finally?

Generally try-finally is used to assure that some piece of code gets executed irrespective if the exception occurs or not. Catch block is generally missing because code in try block does not throw any checked exception which can be caught.

Does try finally catch exceptions?

Yes, it absolutely will. Assuming your finally block doesn't throw an exception, of course, in which case that will effectively "replace" the one that was originally thrown.

Can we use try and finally without catch in C#?

The finally block does not contain any return, continue, break statements because it does not allow controls to leave the finally block. You can also use finally block only with a try block means without a catch block but in this situation, no exceptions are handled.

Does finally execute before catch?

finally defines a block of code we use along with the try keyword. It defines code that's always run after the try and any catch block, before the method is completed. The finally block executes regardless of whether an exception is thrown or caught.


1 Answers

From MSDN, using Statement (C# Reference)

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();   } } 

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.

So go with option 2.

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

like image 83
chakrit Avatar answered Sep 30 '22 20:09

chakrit