This project is read-only.

using() enhancement

Topics: C# Language Design
Sep 14, 2014 at 4:48 PM
Just code:
public interface IScope
{
  void OnSuccess();
  void OnException(Exception ex);
  void OnComplete();
}

...

using (Scope.Enter())
{
  // Some code
}

// Expanded to
IScope p = Scope.Enter();
try
{
  // Some code
  p.OnSuccess();
}
catch (Exception ex)
{
  p.OnException(ex);
  throw;
}
finally
{
  p.OnComplete();
}

// Typical usage
using (conn.EnterTransaction())
{
  // Some code
}
Sep 15, 2014 at 12:31 AM
I like it, and I can see a few cases where this would be useful.

Interestingly, we already have an interface IObserver<T> that looks very similar... perhaps that could be used.
Sep 15, 2014 at 5:18 AM
The concept I've proposed would be to have an interface IDisposableExOnly which would include a member Dispose(Exception ex); that would be invoked with a null exception parameter to affirm that the using block completed normally, or else with the exception that occurred within the using block. Additionally I would define IDispoableEx : IDisposable, IDisposableExOnly; if a class implemented IDisposableEx, the expectation would be that IDisposable.Dispose() method would only be invoked by older versions of the compiler, and would not imply success or failure.
Sep 15, 2014 at 3:56 PM
AndrewVK,

There has been discussion quite a bit earlier in the year. If I have time today I'll try to summarize those discussions here to aid future readers.

Take a look at these :