To prevent Threads using a method at the same time and messing things up Java programmers usually use the synchronized statement when declaring a method.
In the C# world lock-blocks can be used to archieve similar effects. But putting a whole function into a block looks nasty, another way is to use this method decorator:
[MethodImpl(MethodImplOptions.Synchronized)]
Please note, that System.Runtime.CompilerServices must be imported first.
0 Responses to “Java-like synchronized methods in C#”