Does this feature exist? Defining my own curly brackets in C#
Posted
by Carlos
on Stack Overflow
See other posts from Stack Overflow
or by Carlos
Published on 2010-05-21T14:34:00Z
Indexed on
2010/05/21
14:40 UTC
Read the original article
Hit count: 321
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.
© Stack Overflow or respective owner