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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T23:34:20+00:00 2026-05-10T23:34:20+00:00

I recently implemented a class like: class TestClass : IDisposable { RegistryKey m_key; public

  • 0

I recently implemented a class like:

class TestClass : IDisposable {     RegistryKey m_key;     public TestClass()     {         m_key = Registry.CurrentUser.OpenSubKey('Software', false);     }      public void Dispose()     {         // m_key.Dispose();         IDisposable disp = m_key;         disp.Dispose();     } } 

If I uncomment the direct call to Dispose, I get error CS0117 (”Microsoft.Win32.RegistryKey’ does not contain a definition for ‘Dispose”). Some Googling led me to this thread, where I learned what was going on, so I now understand the mechanics of it. The MSDN documentation suggests that the author would prefer that I call Close() instead of Dispose(), but doesn’t explain why.

What is the purpose of this pattern (which I think I’ve seen it in the IO classes as well)? In light of the fact that this was an intentional decision by the class author, how bad is the code above (the call to Dispose through the IDisposable interface)? It can’t be too bad – after all, it’s what would happen in a using statement, right?

[edits: 1) changed title from ‘non-public’ to ‘explicit’ 2) removed the explicit implementation from my code, accidentally left in from experimentation]

  • 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. 2026-05-10T23:34:21+00:00Added an answer on May 10, 2026 at 11:34 pm

    This is called explicit interface implementation. In your example since you define the Dispose() method as ‘void IDisposable.Dispose()’ you are explicitly implementing the IDisposable interface as well.

    This is normally done to avoid collisions. If Microsoft ever wanted to add another Dispose() method that did something else to RegistryKey they wouldn’t be able to unless they used explicit implementation of that interface.

    This is done often with the generic IEnumerable<T> interface. It requires you to also implement the non-generic interface IEnumerable. The only member in these two interfaces is GetEnumerator, with the generic one being more useful, so its usually implemented like this:

    public clas SomeClass : IEnumerable<SomeOtherClass> {     public IEnumerator<SomeOtherClass> GetEnumerator ()     {         ...     }      IEnumerator IEnumerable.GetEnumerator ()     {         return GetEnumerator ();     } } 

    This way when you call an object of SomeClass’s GetEnumator method, it calls the generic version, since the other one was implemented explicitly, allowing us to get the strong-typing generics allow.

    See pages 166-169 of Programming C# by Jesse Liberty (I’ve got the fourth edition).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 70k
  • Answers 70k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • added an answer Just adding the assembly creates a reference, but you have… May 11, 2026 at 1:03 pm
  • added an answer class AutoVivification(dict): '''Implementation of perl's autovivification feature.''' def __getitem__(self, item):… May 11, 2026 at 1:03 pm
  • added an answer Yes, basically you're using the tools wrong. xgettext extracts the… May 11, 2026 at 1:03 pm

Related Questions

I'm a beginner (self-learning) programmer learning C++, and recently I decided to implement a
Recently I've been thinking about finite state machines (FSMs), and how I would implement
I recently printed out Jeff Atwood's Understanding The Hardware blog post and plan on
I recently asked a question about IIf vs. If and found out that there
The question I want to ask is thus: Is casting down the inheritance tree
I have some status data that I want to cache from a database. Any
Recently I've been designing a Thread class library, I've made a Thread abstract class

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.