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 679627
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T01:16:52+00:00 2026-05-14T01:16:52+00:00

I am working with Active Directory using C#. Instantiating the PrincipalContext object seems to

  • 0

I am working with Active Directory using C#. Instantiating the PrincipalContext object seems to be expensive, so I’d like to store one in a class variable.

When using PrincipalContext as a local variable, I can use the convenient using syntax. When storing an IDisposable object in a static variable, how do I ensure the object is properly disposed of?

  • 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-14T01:16:52+00:00Added an answer on May 14, 2026 at 1:16 am

    The general pattern for this is to implement the IDisposable interface on your class. Take this example:

    public class YourClass : IDisposable
    {
        private OtherDisposableType yourResource;
    
        public YourClass()
        {
            yourResource = new OtherDisposableType();
        }
    
        public void Dispose()
        {
            yourResource.Dispose();
        }
    }
    

    This is, at a minimum, what you need to do.

    EDIT

    My previous version advocated following the finalizer pattern in all cases, which was (correctly) pointed out to be against the framework design guidelines. However, in the event that you’re actually dealing with unmanaged resources (for example, you’re making direct P/Invoke calls and obtaining a handle that needs to be explicitly freed) it’s advisable that you create a finalizer and call Dispose within it to protect against people who consume your code and don’t call Dispose:

    public class YourClass : IDisposable
    {
        private OtherDisposableType yourResource;
    
        public YourClass()
        {
            yourResource = new OtherDisposableType();
        }
    
        public void Dispose()
        {
            yourResource.Dispose();
    
            GC.SuppressFinalize(this);
        }
    
        ~YourClass()
        {
            Dispose();
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have some code using DirectoryEntry to manipulate the local Active Directory via LDAP.
I have been working with System.DirectoryServices for a while in a project involving Active
I have active directory and several client computers joined the active directory. In the
Im doing some queries to a active directory, building up my own Dictionary to
We are testing using security realms with our web application. In test we will
Situation We're using PHPUnit in our project and are using a phpunit.xml to ensure
I'm working on a web-based project that users will access after having been authenticated
Currently I'm working on an ASP.NET MVC 2 website. Company policy says that as
I'm trying to connect from Ruby 1.8.7 on RHEL 5 to MS SQL Server.
I have a .NET library that I'm trying to use via COM (hMailServer's VBScript

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.