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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T01:24:47+00:00 2026-05-14T01:24:47+00:00

Consider the following code: // module level declaration Socket _client; void ProcessSocket() { _client

  • 0

Consider the following code:

// module level declaration
Socket _client;

void ProcessSocket() {
    _client = GetSocketFromSomewhere();
    using (_client) {
        DoStuff();  // receive and send data

        Close();
    }
}

void Close() {
    _client.Close();
    _client = null;
}

Given that that the code calls the Close() method, which closes the _client socket and sets it to null, while still inside the `using’ block, what exactly happens behind the scenes? Does the socket really get closed? Are there side effects?

P.S. This is using C# 3.0 on the .NET MicroFramework, but I suppose the c#, the language, should function identically. The reason i am asking is that occasionally, very rarely, I run out of sockets (which is a very precious resource on a .NET MF devices).

  • 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:24:48+00:00Added an answer on May 14, 2026 at 1:24 am

    Dispose will still be called. All you are doing is pointing the variable _client to something else in memory (in this case: null). The object that _client intially referred to will still be disposed at the end of the using statement.

    Run this example.

    class Program
    {
        static Foo foo = null;
    
        static void Main(string[] args)
        {
            foo = new Foo();
    
            using (foo)
            {
                SomeAction();
            }
    
            Console.Read();
        }
    
        static void SomeAction()
        {
            foo = null;
        }
    }
    
    class Foo : IDisposable
    {
        #region IDisposable Members
    
        public void Dispose()
        {
            Console.WriteLine("disposing...");
        }
    
        #endregion
    }
    

    Setting the variable to null is not destroying the object or preventing it from being disposed by the using. All you are doing is changing the reference of the variable, not changing the object originally referenced.

    Late edit:

    Regarding a discussion from the comments about MSDN’s using reference http://msdn.microsoft.com/en-us/library/yh598w02.aspx and the code in the OP and in my example, I created a simpler version of the code like this.

    Foo foo = new Foo();
    using (foo)
    {
        foo = null;
    }
    

    (And, yes, the object still gets disposed.)

    You could infer from the link above that the code is being rewritten like this:

    Foo foo = new Foo();
    {
        try
        {
            foo = null;
        }
        finally
        {
            if (foo != null)
                ((IDisposable)foo).Dispose();
        }
    }
    

    Which would not dispose the object, and that does not match the behavior of the code snippet. So I took a look at it through ildasm, and the best I can gather is that the original reference is being copied into a new address in memory. The statement foo = null; applies to the original variable, but the call to .Dispose() is happening on the copied address. So here is a look at how I believe the code is actually being rewritten.

    Foo foo = new Foo();
    {
        Foo copyOfFoo = foo;
        try
        {
            foo = null;
        }
        finally
        {
            if (copyOfFoo != null)
                ((IDisposable)copyOfFoo).Dispose();
        }
    }
    

    For reference, this is what the IL looks like through ildasm.

    .method private hidebysig static void  Main() cil managed
    {
      .entrypoint
      // Code size       29 (0x1d)
      .maxstack  1
      .locals init ([0] class Foo foo,
               [1] class Foo CS$3$0000)
      IL_0000:  newobj     instance void Foo::.ctor()
      IL_0005:  stloc.0
      IL_0006:  ldloc.0
      IL_0007:  stloc.1
      .try
      {
        IL_0008:  ldnull
        IL_0009:  stloc.0
        IL_000a:  leave.s    IL_0016
      }  // end .try
      finally
      {
        IL_000c:  ldloc.1
        IL_000d:  brfalse.s  IL_0015
        IL_000f:  ldloc.1
        IL_0010:  callvirt   instance void [mscorlib]System.IDisposable::Dispose()
        IL_0015:  endfinally
      }  // end handler
      IL_0016:  call       int32 [mscorlib]System.Console::Read()
      IL_001b:  pop
      IL_001c:  ret
    } // end of method Program::Main
    

    I don’t make a living staring at ildasm, so my analysis can be classified as caveat emptor. However, the behavior is what it is.

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

Sidebar

Ask A Question

Stats

  • Questions 404k
  • Answers 404k
  • 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
  • Editorial Team
    Editorial Team added an answer Can you give the class a more consistent name? Like… May 15, 2026 at 5:38 am
  • Editorial Team
    Editorial Team added an answer (My previous answer was ill-informed, I did not notice at… May 15, 2026 at 5:38 am
  • Editorial Team
    Editorial Team added an answer The second <script> contains code that depends on google-analytics.com/ga.js loading.… May 15, 2026 at 5:38 am

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.