Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 72321
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T20:01:35+00:00 2026-05-10T20:01:35+00:00

I’ve got a bunch of properties which I am going to use read/write locks

  • 0

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();     } } 
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. 2026-05-10T20:01:35+00:00Added an answer on May 10, 2026 at 8:01 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to understand how to use SyndicationItem to display feed which is
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I used javascript for loading a picture on my website depending on which small
I have a jquery bug and I've been looking for hours now, I can't
I've got a string that has curly quotes in it. I'd like to replace
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I want use html5's new tag to play a wav file (currently only supported
I would like to run a str_replace or preg_replace which looks for certain words
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I have a text area in my form which accepts all possible characters from

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.