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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T01:39:33+00:00 2026-06-01T01:39:33+00:00

Context: I have a class that implements a Session, maintaining a TCP connection to

  • 0

Context: I have a class that implements a “Session”, maintaining a TCP connection to a server during it’s lifetime. In the constructor, I create a TcpClient instance with a given IP address and port of a server to connect to.

   public Session(IPAddress ipAddress)
   {
       Client = new TcpClient(ipAddress.ToString(), 1234); //create a client to a server which we will later use to work with
       DataStream = Client.GetStream(); //get the stream for later usage
   }

Objects of this session will occasionally send and receive data to and from their associated server.

My question now is: Where should I close the TcpClient and its underlying NetworkStream?

Should I implement IDisposable? But What happens if the user of my class forgets to call Dispose?

Should I implement a Destructor (aka Finalizer)? But AFAIK I should never access managed resources from there?

Thanks for helping me out!

  • 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-06-01T01:39:34+00:00Added an answer on June 1, 2026 at 1:39 am

    Should I implement IDisposable?

    Yes

    But What happens if the user of my class forgets to call Dispose?

    you should follow the Disposable pattern as defined here: http://msdn.microsoft.com/en-us/library/b1yfkh5e.aspx

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

Sidebar

Related Questions

I have a session-scoped bean in Spring that is set within the web context.
I have a class MyClass that has a static variable. Class implements smth like
I have a class that implements IAuthorizationPolicy. I set up a custom Principal object
I have an initialization class that preloads content into a variable (probably a list
i have an application that loads the page content. I use the WebClient class.
I have a context loader class which loads an XML file with info on
I have the following code in my context, and no explicit table-class mapping, yet
I have a relatively small Java library that implements a few dozen beans (no
i have a strange behaviour i'm seeing that if the user access to session
I've my main custom listactivity class that have a instance object of my custom

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.