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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T23:21:35+00:00 2026-05-18T23:21:35+00:00

interface IMyInterace { void Open(); object Read(); void Close(); } class MyImplementation : IMyInterface

  • 0
interface IMyInterace
{
void Open();
object Read();
void Close();
}

class MyImplementation : IMyInterface
{
public void Open() { /* instantiates disposible class */ }
//...
public void Close() { /* calls .Dispose(); */ }

}

Is there a good way to deal with this type of situation to ensure that disposible instances inside the class get called? (There is no signal to callers that they must call ‘Close’ except in documentation.) Implementations of IMyInterface do not necessarily encapsulate IDisposible instances and are closed and reopened repeatedly throughout the application’s lifetime.

I’m thinking of doing this:

  • Implement IDisposible in MyImplementation.
  • Set Dispose() to call Close().
  • Add a call to Close() or Dispose() to the
    begining of Open to ensure previous
    call was closed.

Users of IMyInterface do not know what implementation they are using, so I’m not sure how much value making MyImplementation disposible has, and again, not all implementations will encapsulate IDisposibles.

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

    In addition to the answers already here:

    If this class is (often/sometimes) used through the interface alone I would advice to inherit IMyInterace from IDisposable.

    That will let your users use these objects in a consistent manner. The drawback is of course that you may need to add (dummy) Dispose methods to classes that don’t actually need it. But the benefit is in consistency and flexibility: What if a class changes in the future so that it does need a Dispose() ?

    A minimal approach:

    interface IMyInterace : IDisposable { }
    
    sealed class MyImplementation : IMyInterface 
    {   
       public void Open() { /* instantiates disposible class */ }
    
       public void Close() { /* calls _myField.Dispose(); */ }
    
       public void Dispose() { Close(); }  // only use this short form in a sealed class
    
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an interface: public interface IMyInterface { } I have a class which
I have method (which is part of IMyInteface) like this: interface IMyInterface { void
I have some interface, and a class implementing this interface, say: interface IMyInterface {
Given this interface public interface IMyInterface { string Method1(); } Why is this valid
If have a set of classes that all implement an interface. interface IMyinterface<T> {
An interface is a 100% abstract class, so we can use an interface for
public interface IBar {} public interface IFoo : IBar {} typeof(IFoo).BaseType == null How
Say I have a C# interface called IMyInterface defined as follows: // C# code
I have a C# interface with certain method parameters declared as object types. However,
How do I retrieve an attribute from an interface method my class implements? Edit:

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.