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 70967
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T19:48:50+00:00 2026-05-10T19:48:50+00:00

I just realized that in some place in my code I have the return

  • 0

I just realized that in some place in my code I have the return statement inside the lock and sometime outside. Which one is the best?

1)

void example() {     lock (mutex)     {     //...     }     return myData; } 

2)

void example() {     lock (mutex)     {     //...     return myData;     }  } 

Which one should I use?

  • 1 1 Answer
  • 3 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-10T19:48:51+00:00Added an answer on May 10, 2026 at 7:48 pm

    Essentially, which-ever makes the code simpler. Single point of exit is a nice ideal, but I wouldn’t bend the code out of shape just to achieve it… And if the alternative is declaring a local variable (outside the lock), initializing it (inside the lock) and then returning it (outside the lock), then I’d say that a simple ‘return foo’ inside the lock is a lot simpler.

    To show the difference in IL, lets code:

    static class Program {     static void Main() { }      static readonly object sync = new object();      static int GetValue() { return 5; }      static int ReturnInside()     {         lock (sync)         {             return GetValue();         }     }      static int ReturnOutside()     {         int val;         lock (sync)         {             val = GetValue();         }         return val;     } } 

    (note that I’d happily argue that ReturnInside is a simpler/cleaner bit of C#)

    And look at the IL (release mode etc):

    .method private hidebysig static int32 ReturnInside() cil managed {     .maxstack 2     .locals init (         [0] int32 CS$1$0000,         [1] object CS$2$0001)     L_0000: ldsfld object Program::sync     L_0005: dup      L_0006: stloc.1      L_0007: call void [mscorlib]System.Threading.Monitor::Enter(object)     L_000c: call int32 Program::GetValue()     L_0011: stloc.0      L_0012: leave.s L_001b     L_0014: ldloc.1      L_0015: call void [mscorlib]System.Threading.Monitor::Exit(object)     L_001a: endfinally      L_001b: ldloc.0      L_001c: ret      .try L_000c to L_0014 finally handler L_0014 to L_001b }   method private hidebysig static int32 ReturnOutside() cil managed {     .maxstack 2     .locals init (         [0] int32 val,         [1] object CS$2$0000)     L_0000: ldsfld object Program::sync     L_0005: dup      L_0006: stloc.1      L_0007: call void [mscorlib]System.Threading.Monitor::Enter(object)     L_000c: call int32 Program::GetValue()     L_0011: stloc.0      L_0012: leave.s L_001b     L_0014: ldloc.1      L_0015: call void [mscorlib]System.Threading.Monitor::Exit(object)     L_001a: endfinally      L_001b: ldloc.0      L_001c: ret      .try L_000c to L_0014 finally handler L_0014 to L_001b } 

    So at the IL level they are [give or take some names] identical (I learnt something ;-p). As such, the only sensible comparison is the (highly subjective) law of local coding style… I prefer ReturnInside for simplicity, but I wouldn’t get excited about either.

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

Sidebar

Related Questions

I have been assigned to work on some legacy C++ code in MFC. One
I have some python code that binds to a socket. I catch any exception
I have a model with a DateTime property that in one place is placed
I just realized that I don't have a answer to this question, when I'm
friends I was just playing around with some pointer programs and realized that the
I just realized that I need to synchronize a significant amount of data collection
I just realized that the method Element.getElementsByTagName(someTagName) returns a nodelist of all elements in
I just realized that i may not be following best practices in regards to
I just realized that when i start a task from within a task and
I just realized that my app, with over 300 users still using an Android

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.