Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3675718
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T03:00:40+00:00 2026-05-19T03:00:40+00:00

I have a class that implements IDisposable because it has a private member field

  • 0

I have a class that implements IDisposable because it has a private member field “foo” that is IDisposable (which is initialized in the constructor). I am unexpectedly getting a CA2000 Code Analysis error, which wants me to be sure to dispose of foo. However, I have foo.Dispose() in my class’s Dispose() code, which should take care of this.

I did some searching around and surprisingly can’t find an answer. What am I doing wrong? Clearly I missing something basic. How do I write my code to overcome this?

My VB code:

Public Class Bar
    Implements IDisposable

    Private Foo As SomeDisposableThing

    Public Sub New()
        Foo = New SomeDisposableThing() With {.name = "hello"}
    End Sub

    '''' snip ''''

    Private disposedValue As Boolean = False        ' To detect redundant calls '

    Protected Overridable Sub Dispose(ByVal disposing As Boolean)
        If Not Me.disposedValue Then
            If disposing Then
                If Foo IsNot Nothing Then Foo.Dispose()
            End If
        End If
        Me.disposedValue = True
    End Sub

    Public Sub Dispose() Implements IDisposable.Dispose
        Dispose(True)
        GC.SuppressFinalize(Me)
    End Sub

End Class
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-19T03:00:40+00:00Added an answer on May 19, 2026 at 3:00 am

    The CA2000 error doesn’t refer to the container implementing IDisposable but rather the use of a local which is not properly disposed. The reason why is that you are using an object initializer on the disposable object. The actual code that will get generated is essentially the following

    Dim temp = New SomethingDisposable()
    temp.Name = "hello"
    Foo = temp
    

    This code is correctly flagged by FxCop as not properly disposing of an IDisposable in all instances (it’s possible for an exception to occur on the temp.Name = "hello" line in which case it wouldn’t be disposed).

    The fix is to not use an object initializer here and initialize Foo directly

    Foo = New SomethingDisposable()
    Foo.Name = "hello"
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Let's say I have a class that implements the IDisposable interface. Something like this:
If I have a SomeDisposableObject class which implements IDisposable : class SomeDisposableObject : IDisposable
I have a class which implements IDisposable interface. using System; class A : IDisposable
Let's say I have a class that has a MyCustomDatabaseAccess as a data member.
I have a class that implements multiple interfaces. I would like to register these
I have a custom class that implements that IComparable. This class is stored in
I have a custom class that implements ICollection , and this class is readonly,
I'm currently writing a class that implements the SeekableIterator interface and have run into
I have the following class which is a decorator for an IDisposable object (I
I have a class which uses a method in user32.dll: [System.Runtime.InteropServices.DllImport(user32.dll)] private static extern

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.