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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T14:03:00+00:00 2026-06-12T14:03:00+00:00

I have the following code block, and it affects my program efficiency. The problem

  • 0

I have the following code block, and it affects my program efficiency. The problem here is if the target host exists, everything is OK. But if it does exist, it takes too long to execute. Finally, I found out the “udp.Close()” occupies the most of execution time. If I does not invoke the close method, the efficiency is good.

Can anyone help me tell me what is the drawback if I do not invoke the close method?? Thank you very much.

{ // This is my code block, I need to execute it many many times.
string ipAddress = Dns.GetHostAddresses("joe-pc").FirstOrDefault().ToString();
UdpClient udp = new UdpClient(ipAddress, Port);
udp.Send(msg, msg.Length);
udp.Close();
udp = null;
}
  • 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-12T14:03:02+00:00Added an answer on June 12, 2026 at 2:03 pm

    The drawback is that you’ll have a resource leak. You may be lucky enough that garbage collection happens often enough that it doesn’t demonstrate itself in your program, but why take the chance? From the documentation on Close:

    The Close disables the underlying Socket and releases all managed and unmanaged resources associated with the UdpClient.

    Note, it talks of unmanaged resources. These will only be released by the UdpClient running some code – it either does it in Close/Dispose, or it has to do it in its Finalize method – nothing else will cause them to be released (assuming the program stays running).

    You may be able to hide the cost of the Close operation by using Task.Run to have it run on another thread – but you’d have to weigh up the cost of doing so.


    Or, to put it in more concrete terms – you say that you need this method to run many times. By not cleaning up your resources here, you would increase the chances that a subsequent call will fail completely because it cannot acquire the required resources (they’re all tied up in existing, non-Closed UdpClient instances).


    And, as indicated in my comment, the following line is pointless:

    udp = null;
    

    Such code used to be of use in COM era VB, but it has no place in the .NET world.

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

Sidebar

Related Questions

Hello everybody I have a problem with exception. The following code block do this
Hi I have following code block public class Driver { static String x =
I have the following code block, as the content of my AppDelegate.h of an
I have the following code block for validation in PHP and I can't figure
I have the following markup / code block in the ASPX file. The binding
I have the following two code blocks. Code block 1 var checkboxes = $(div.c1
I have the following code. Within the same block of code, I disable a
I need syntax help with the following code logic: I have a code block
I have the following code in a client supplied block: $user = $_POST['user']; $sql
I have the following code a.rollover { background-image: url('sprite.jpg'); display: block; width: 191px; height:

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.