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

  • Home
  • SEARCH
  • 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'm working on a PHP/Active Directory auth class, but would like to know how
I'm working on importing groups and users from active directory using sql server. I've
I have some code using DirectoryEntry to manipulate the local Active Directory via LDAP.
I am working on a project using Castle Active Record. I stumbled across the
I am writing some Java code that authenticates to Active Directory using SASL GSSAPI.
The project I am working on will integrate with the customers Active Directory in
I'm working on taking a date value (createWhen) from Active Directory, and translating it
I'm trying to create a new user on my development active directory server using
I'm working on an application that talks to Active Directory through the LDAP provider,
I am attempting to get a test Kerberos client/server pair working against Active Directory.

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.