You'll appreciate the following two syntactic sugars:
lock(obj)
{
//Code
}
same as:
Monitor.Enter(obj)
try
{
//Code
}
finally
{
Monitor.Exit(obj)
}
and
using(var adapt = new adapter()){
//Code2
}
same as:
var adapt= new adapter()
try{
//Code2
}
finally{
adapt.Dispose()
}
Clearly the first example in each case is more readable. Is there a way to define this kind of thing myself, either in the C# language, or in the IDE? The reason I ask is that there are many similar usages (of the long kind) that would benefit from this, eg. if you're using ReaderWriterLockSlim, you want something pretty similar.
EDIT 1:
I've been asked to provide an example, so I'll give it a go:
myclass
{
ReaderWriterLockSlim rwl = new ReaderWriterLockSlim();
void MyConcurrentMethod()
{
rwl.EnterReadLock();
try{
//Code to do in the lock, often just one line, but now its turned into 8!
}
finally
{
rwl.ExitReadLock();
}
}
}
//I'd rather have:
void MyConcurrentMethod()
{
rwl.EnterReadLock()
{
//Code block. Or even simpler, no brackets like one-line ifs and usings
}
}
Of course you'd have to give some thoughts as to how to use the TryEnterReadLocks and those kinds of things with returns. But I'm sure you could think of something.