Dynamics of the using keyword
Posted
by AngryHacker
on Stack Overflow
See other posts from Stack Overflow
or by AngryHacker
Published on 2010-03-25T04:20:19Z
Indexed on
2010/03/25
4:23 UTC
Read the original article
Hit count: 435
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).
© Stack Overflow or respective owner