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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T00:49:28+00:00 2026-05-26T00:49:28+00:00

Summary : if I create an object in a constructor initialiser, how do I

  • 0

Summary: if I create an object in a constructor initialiser, how do I keep a reference to it so I can reference it later?

Details:

I have a class (LibBase, below) that requires a StreamWriter as its construction parameter. I don’t have the source code for LibBase – its in a third-party library.

public class LibBase
{
    public LibBase(System.IO.StreamWriter wtr) { ... }
}

I have derived MyClass from LibBase and in the MyClass constructor I want to pass an instance of MyWriter (derived form StreamWriter) to the base class. I do this as follows.

public class MyWriter : System.IO.StreamWriter
{
    public MyWriter(int n) { ... }
    // Contains unmanaged resources
}

public class MyClass : LibBase
{
    public MyClass(int n)
    : LibBase(new MyWriter(n))
    { }
}

The problem is that MyWriter requires disposing, so MyClass should dispose it (and implement IDisposable to do this) but MyClass doesn’t have a reference to the created MyWriter instance, so I can’t dispose it. The syntax for the constructor initialiser doesn’t seem to permit my to keep a reference.

My solution is to re-code MyClass as follows:

public class MyClass : LibBase, IDisposable
{
    public MyClass(Encoding enc)
    : this(new MyWriter(enc))
    { }

    private MyClass(MyWriter wtr)
    : LibBase(wtr)
    { this.wtr = wtr; }  // store reference

    private MyWriter wtr;

    // (implement IDisposable using wtr member variable
}

The private constructor stores a reference to the MyWriter instance so I can dispose it later.

My questions:

  1. What am I missing here? I feel like I’m fighting the language. Does C# provide a better way to do this?
  2. If the language doesn’t directly support this, is there a better solution than my private constructor technique?
  3. Any comments on defects in my solution?
  • 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-26T00:49:28+00:00Added an answer on May 26, 2026 at 12:49 am

    Your solution seems ok… I don’t think that you miss anything…

    IF you want to change the implementation (for whatever reason):

    • You could implement that by not inheriting from LibBase but having an instance as a private member…
    • another option would be to implement the Factory pattern for MyClass thus having no public constructor and creating the StreamWriter instance factory-side etc.

    BUT as I said there is nothing wrong with you solution (if it happens often you probably should rethink your design).

    EDIT – as per comment:

    What I mean by “creating StreamWriter factory-side” is: create a Factory for MyClass so that anyone needing an instance uses the Factory… therein you can create the StreamWriter instance in the Factory method and pass it in as param to MyClass… this way you could even implement some fancy things like “which MyClass instance is using a given StreamWriter instance ?” or some sort of a cache for MyClass / StreamWriter instances etc.

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

Sidebar

Related Questions

I have a handle-based class that I need to create a vector of. An
I have a custom object that I am trying to bind to a control.
I have object Project in my entity. I create project by: public static void
Here at work we have developed a SOAP WCF API that can be reached
I have a class that has overloaded constructors. I'd like to re-factor the code
Data table structure is: id1,id2,id3,id4,... (some other fields). I want to create summary query
Summary: Can I program a thick client game in C without reinventing wheels ,
I need to have a summary field in each page of the report and
I have a winforms project which allows future developers to create plugins for the
I have a constructor which has a non-interface dependency: public MainWindowViewModel(IWorkItemProvider workItemProvider, WeekNavigatorViewModel weekNavigator)

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.