james mckay dot net

because there are few things that are less logical than business logic

Why doesn’t C# have a synchronized keyword?

In Java you can mark a class method as a critical section, i.e. it will only be executed by one thread at a time, by using the synchronized keyword:

[code lang=”java”]
synchronized void myMethod()
{
doSomething();
}
[/code]

In C# you have to type in this ghastly (and rather obscure) mouthful:

[code lang=”csharp”]
using System.Runtime.CompilerServices;

[MethodImpl(MethodImplOptions.Synchronized)]
void MyMethod()
{
DoSomething();
}
[/code]

Why can’t we have a synchronized keyword in C# for this like in Java?