question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Auto-implemented IDisposable for classes with disposable fields.

See original GitHub issue

Currently, implementing IDisposable requires a significant amount of boiler plate code. I would like to propose a way to auto-implement IDisposable for fields. Using the following syntax:

public class Foo : IDisposable
{
    private using Bar _barResource;
}

Here the using keyword is being used as a modifier on the field. It indicates that the field should be disposed when the Dispose method is called. These fields would act the same way as readonly fields in that they can only be set from inside constructors or with field initializers.

This syntax would then have the compiler auto-implement IDisposable that handles disposing all fields marked using and also a finalizer to call Dispose if it has not yet been called. The above code sample would expand into the typical dispose pattern.

There are a few considerations that need to be made, though. These include, but are not limited to:

  • What to do with potential circular references between resources?
  • How to handle dispose logic that doesn’t fit the typical pattern?
  • How to differentiate between managed and unmanaged disposable resources?

The value I could see this feature providing is a reduction in the amount of code needed to write classes that are composed of IDisposable resources to ensure proper cleanup of those resources.

Issue Analytics

  • State:closed
  • Created 9 years ago
  • Reactions:1
  • Comments:8 (6 by maintainers)

github_iconTop GitHub Comments

1reaction
sharwellcommented, Jan 28, 2015

I would lean towards:

private disposable Bar _barResource;
  • What to do with potential circular references between resources?

The generated implementation of Dispose(bool) could be implemented in a manner where recursive calls are simply ignored.

  • How to handle dispose logic that doesn’t fit the typical pattern?

Probably in a manner similar to constructors. For example, the following doesn’t cause a problem:

public class Foo
{
  object _bar = new object();

  public Foo()
  {
    // things here
  }
}
  • How to differentiate between managed and unmanaged disposable resources?

There is no need to. Only allow this syntax on a field where the static type of the field is a type that implements IDisposable. These are always managed objects.

0reactions
gaftercommented, Mar 24, 2016

We are unlikely to provide direct language support for this, however, we expect there will likely be solutions built on #5292/#5561.

Read more comments on GitHub >

github_iconTop Results From Across the Web

c# - Are disposable fields of disposable classes ...
IDisposable is an interface and a pattern. It's not really part of the C# language, outside of explicit support in using and foreach...
Read more >
CA1001: Types that own disposable fields should be ...
A class that declares an IDisposable field indirectly owns an unmanaged resource. The class should implement the IDisposable interface to ...
Read more >
How to properly implement idisposable (c#)?
I ran Analyze > Run Code Analysis on Solution on my C# Winforms app. I got this: CA1001 Types that own disposable fields...
Read more >
All about IDisposable
Disposing of objects and IDisposable interface are fundamental concepts of .NET Framework. In C# we have multiple ways to use disposable objects ......
Read more >
Properly Implementing The IDisposable Interface
In this article, I will show the proper way to implement the IDisposable interface in types that you create that contain disposable fields....
Read more >

github_iconTop Related Medium Post

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Hashnode Post

No results found