Implicitly scoped lock statement #2451
-
Motivation:Allow Before:public void Foo() {
lock (numberGate) {
number += 5;
if (number > 1000) {
number = 0;
}
}
} After:public void Foo() {
lock numberGate;
number += 5;
if (number > 1000) {
number = 0;
}
} |
Beta Was this translation helpful? Give feedback.
Replies: 12 comments 9 replies
-
It is absolutely vital that locks are as short as possible. As such this might be considered a bad idea. |
Beta Was this translation helpful? Give feedback.
-
Same rationale as was used for simplifying |
Beta Was this translation helpful? Give feedback.
-
@marksmeltzer wrote:
The simplification for using makes sense because the vast majority of using statements finish at the end of the enclosing block:
However, because of the need for lock statements to enclose as little as possible, my experience is that those blocks typically finish well before the enclosing block:
Creating a variation of lock that encourages longer lock retention would be a bad idea. I'd also offer the observation that using statements are extremely common - so the new using declarations will benefit a lot of developers. By contrast, lock statements are relatively rare, so any benefit would accrue to a significantly smaller group. |
Beta Was this translation helpful? Give feedback.
-
It's also more common to end up with many using statements in the same method, so using statements can keep the number of blocks and levels of indentation required under control in a method when omitting the blocks isn't possible. |
Beta Was this translation helpful? Give feedback.
-
Another difference between |
Beta Was this translation helpful? Give feedback.
-
Yes you can. |
Beta Was this translation helpful? Give feedback.
-
I'm referring to the new feature slated for C# 8.0: void M() {
using var foo = new Foo();
// do stuff here
} // foo automatically disposed here Sorry, should have clarified. |
Beta Was this translation helpful? Give feedback.
-
@theunrepentantgeek for me when using locks, is common that the scope of locks are the method/property itself. Another side effect is to provide a feature closer to Java synchronized methods. |
Beta Was this translation helpful? Give feedback.
-
This already exists in .NET: [MethodImpl(MethodImplOptions.Synchronized)]
public void Method()
{
MethodImpl();
} C# doesn't expose it at the method level because it behaves identically to how This is really a sidebar as I assume your proposal would keep to the concept of locking on some arbitrary object and not trying to replicate Java |
Beta Was this translation helpful? Give feedback.
-
Please consider fixed for the same feature |
Beta Was this translation helpful? Give feedback.
-
public void Foo()
{
lock (numberGate) restOfFoo();
void restOfFoo()
{
number += 5;
if (number > 1000) number = 0;
}
} 🤡 |
Beta Was this translation helpful? Give feedback.
-
and
I think that's actually a descision of the developer and has nothing to do with the syntax. The argument misses the real motivation behind this proposal. It's about making code more readable and easy to write. One circumstance that may complicate the implementation of the suggestion is the fact that |
Beta Was this translation helpful? Give feedback.
@marksmeltzer wrote:
The simplification for using makes sense because the vast majority of using statements finish at the end of the enclosing block:
However, because of the need for lock statements to enclose as little as possible, my experience is that those blocks typically finish well before the enclosing block:
Creating a variation of lock that encourages longer lock retention would be a bad idea.
I'd also offer the observation that u…