Skip to main content
AI Assist is now on Stack Overflow. Start a chat to get instant answers from across the network. Sign up to save and share your chats.
Applied some formatting, etc.
Source Link
Peter Mortensen
  • 31.4k
  • 22
  • 110
  • 134

usingusing, in the sense of

using (var foo = new Bar()) { Baz(); } 

Is actually shorthand for a try/finally block. It is equivalent to the code:

var foo = new Bar(); try { Baz(); } finally { foo.Dispose(); } 

You'll note, of course, that the first snippet is much more concise than the second and also that there are many kinds of things that you might want to do as cleanup even if an exception is thrown. Because of this, we've come up with a class that we call ScopeScope that allows you to execute arbitrary code in the Dispose method. So, for example, if you had a property called IsWorking that you always wanted to set to false after trying to perform an operation, you'd do it like this:

using (new Scope(() => IsWorking = false)) { IsWorking = true; MundaneYetDangerousWork(); } 

You can read more about our solution and how we derived it here.

using, in the sense of

using (var foo = new Bar()) { Baz(); } 

Is actually shorthand for a try/finally block. It is equivalent to the code:

var foo = new Bar(); try { Baz(); } finally { foo.Dispose(); } 

You'll note, of course, that the first snippet is much more concise than the second and also that there are many kinds of things that you might want to do as cleanup even if an exception is thrown. Because of this, we've come up with a class that we call Scope that allows you to execute arbitrary code in the Dispose method. So, for example, if you had a property called IsWorking that you always wanted to set to false after trying to perform an operation, you'd do it like this:

using (new Scope(() => IsWorking = false)) { IsWorking = true; MundaneYetDangerousWork(); } 

You can read more about our solution and how we derived it here.

using, in the sense of

using (var foo = new Bar()) { Baz(); } 

Is actually shorthand for a try/finally block. It is equivalent to the code:

var foo = new Bar(); try { Baz(); } finally { foo.Dispose(); } 

You'll note, of course, that the first snippet is much more concise than the second and also that there are many kinds of things that you might want to do as cleanup even if an exception is thrown. Because of this, we've come up with a class that we call Scope that allows you to execute arbitrary code in the Dispose method. So, for example, if you had a property called IsWorking that you always wanted to set to false after trying to perform an operation, you'd do it like this:

using (new Scope(() => IsWorking = false)) { IsWorking = true; MundaneYetDangerousWork(); } 

You can read more about our solution and how we derived it here.

using, in the sense of

using (var foo = new Bar()) { Baz(); } 

Is actually shorthand for a try/finally block. It is equivalent to the code:

var foo = new Bar(); try { Baz(); } finally { foo.Dispose(); } 

You'll note, of course, that the first snippet is much more concise than the second and also that there are many kinds of things that you might want to do as cleanup even if an exception is thrown. Because of this, we've come up with a class that we call Scope that allows you to execute arbitrary code in the Dispose method. So, for example, if you had a property called IsWorking that you always wanted to set to false after trying to perform an operation, you'd do it like this:

using (new Scope(() => IsWorking = false)) { IsWorking = true; MundaneYetDangerousWork(); } 

You can read more about our solution and how we derived it herehere.

using, in the sense of

using (var foo = new Bar()) { Baz(); } 

Is actually shorthand for a try/finally block. It is equivalent to the code:

var foo = new Bar(); try { Baz(); } finally { foo.Dispose(); } 

You'll note, of course, that the first snippet is much more concise than the second and also that there are many kinds of things that you might want to do as cleanup even if an exception is thrown. Because of this, we've come up with a class that we call Scope that allows you to execute arbitrary code in the Dispose method. So, for example, if you had a property called IsWorking that you always wanted to set to false after trying to perform an operation, you'd do it like this:

using (new Scope(() => IsWorking = false)) { IsWorking = true; MundaneYetDangerousWork(); } 

You can read more about our solution and how we derived it here.

using, in the sense of

using (var foo = new Bar()) { Baz(); } 

Is actually shorthand for a try/finally block. It is equivalent to the code:

var foo = new Bar(); try { Baz(); } finally { foo.Dispose(); } 

You'll note, of course, that the first snippet is much more concise than the second and also that there are many kinds of things that you might want to do as cleanup even if an exception is thrown. Because of this, we've come up with a class that we call Scope that allows you to execute arbitrary code in the Dispose method. So, for example, if you had a property called IsWorking that you always wanted to set to false after trying to perform an operation, you'd do it like this:

using (new Scope(() => IsWorking = false)) { IsWorking = true; MundaneYetDangerousWork(); } 

You can read more about our solution and how we derived it here.

Source Link
Amanda Mitchell
  • 2.7k
  • 1
  • 17
  • 23

using, in the sense of

using (var foo = new Bar()) { Baz(); } 

Is actually shorthand for a try/finally block. It is equivalent to the code:

var foo = new Bar(); try { Baz(); } finally { foo.Dispose(); } 

You'll note, of course, that the first snippet is much more concise than the second and also that there are many kinds of things that you might want to do as cleanup even if an exception is thrown. Because of this, we've come up with a class that we call Scope that allows you to execute arbitrary code in the Dispose method. So, for example, if you had a property called IsWorking that you always wanted to set to false after trying to perform an operation, you'd do it like this:

using (new Scope(() => IsWorking = false)) { IsWorking = true; MundaneYetDangerousWork(); } 

You can read more about our solution and how we derived it here.