Does this feature exist? Defining my own curly brackets in C#
- by Carlos
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.