By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,678 Members | 1,158 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,678 IT Pros & Developers. It's quick & easy.

Proper use of the "using" statement

P: n/a
Hi all,

I have a class SwitchCursor, that implements IDisposable. Is it legal
to use an object that is not assigned to a reference inside a using
statement like this:

using(new SwitchCursor())// is the SwitchCursor safe from GC?
{
}

or is the Object eligible for GC?

Thanks in advance,
Andy
Nov 15 '05 #1
Share this Question
Share on Google+
10 Replies


P: n/a
Andreas Müller <an***************@gmx.de> wrote:
I have a class SwitchCursor, that implements IDisposable. Is it legal
to use an object that is not assigned to a reference inside a using
statement like this:

using(new SwitchCursor())// is the SwitchCursor safe from GC?
{
}

or is the Object eligible for GC?


You can't do that, as the syntax for the using construct requires that
you assign the value to a variable.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 15 '05 #2

P: n/a
Jon,

Actually, you can do that. According to section 8.13 (The using
statement) of the C# spec, the using keyword only requires a valid
expression or a local variable declaration. I think that the C#
programmer's reference is incorrect.

The following code:

using (new System.Data.DataSet())
{
}

will compile. It also creates an implicit variable declaration which it
is stored in. This will keep it from being GCed until the using block
exits.

Hope this helps.

--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
Andreas Müller <an***************@gmx.de> wrote:
I have a class SwitchCursor, that implements IDisposable. Is it legal
to use an object that is not assigned to a reference inside a using
statement like this:

using(new SwitchCursor())// is the SwitchCursor safe from GC?
{
}

or is the Object eligible for GC?


You can't do that, as the syntax for the using construct requires that
you assign the value to a variable.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 15 '05 #3

P: n/a
I believe it is safe for SwitchCursor to be used this way.

Tu-Thach
-----Original Message-----
Hi all,

I have a class SwitchCursor, that implements IDisposable. Is it legalto use an object that is not assigned to a reference inside a usingstatement like this:

using(new SwitchCursor())// is the SwitchCursor safe from GC?{
}

or is the Object eligible for GC?

Thanks in advance,
Andy
.

Nov 15 '05 #4

P: n/a
Nicholas Paldino [.NET/C# MVP] <mv*@spam.guard.caspershouse.com> wrote:
Actually, you can do that. According to section 8.13 (The using
statement) of the C# spec, the using keyword only requires a valid
expression or a local variable declaration. I think that the C#
programmer's reference is incorrect.

The following code:

using (new System.Data.DataSet())
{
}

will compile. It also creates an implicit variable declaration which it
is stored in. This will keep it from being GCed until the using block
exits.


Ah - I was misreading the ECMA spec. Doh :(

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 15 '05 #5

P: n/a
How would that be used? I mean if you can't access that dataset since you
don't know the implicit variable name what is the purpose of creating it?

"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.com> wrote in
message news:eJ**************@TK2MSFTNGP09.phx.gbl...
Jon,

Actually, you can do that. According to section 8.13 (The using
statement) of the C# spec, the using keyword only requires a valid
expression or a local variable declaration. I think that the C#
programmer's reference is incorrect.

The following code:

using (new System.Data.DataSet())
{
}

will compile. It also creates an implicit variable declaration which it is stored in. This will keep it from being GCed until the using block
exits.

Hope this helps.

Nov 15 '05 #6

P: n/a
Daniel Billingsley <db**********@NO.durcon.SPAAMM.com> wrote:
How would that be used? I mean if you can't access that dataset since you
don't know the implicit variable name what is the purpose of creating it?


It could be used for locking purposes, or possibly tracing purposes.
For instance:

using (new TraceBlock ("doing something"))
{
...
}

where TraceBlock would have a static thread-local stack of "names" in
it, which could be dumped if you wanted. Creating a new TrackBlock
would add the name to the stack, and disposing it would pop the top
element.

That's just an example off the top of my head, and it's not terribly
good. I'm sure it's quite rare, but there are probably a few real cases
around :)

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 15 '05 #7

P: n/a
On Wed, 15 Oct 2003 12:43:18 +0100, Jon Skeet [C# MVP] <sk***@pobox.com>
wrote:
Daniel Billingsley <db**********@NO.durcon.SPAAMM.com> wrote:
How would that be used? I mean if you can't access that dataset since
you
don't know the implicit variable name what is the purpose of creating
it?


It could be used for locking purposes, or possibly tracing purposes.
For instance:


Another possible use might be with a product such as Raize CodeSite, in
which you could scope tracing. The constructor would then log the entry
into the scope, and the destructor/finalize method would log the exit out
of the scope. This could provide timing information as well as provide you
with a stack-trace like log for evidence gathering for a particularly
hard-to-find problem. Just a thought anyway.

(To Jon: Sorry about the email, I tried the "Quick Reply" of Opera and it
used email reply in a newsgroup)

--
Lasse Vågsæther Karlsen
http://www.vkarlsen.no/
PGP KeyID: 0x0270466B
Nov 15 '05 #8

P: n/a
[Please do not mail me a copy of your followup]

Jon Skeet [C# MVP] <sk***@pobox.com> spake the secret code
<MP************************@msnews.microsoft.com > thusly:
That's just an example off the top of my head, and it's not terribly
good. I'm sure it's quite rare, but there are probably a few real cases
around :)


There are lots of examples where anonymous items in a using statement
are useful. Basically, any time you have to do something like this:

try
{
StartSomething();
// do stuff
}
finally
{
EndSomething();
}

You can encapsulate the Start/End inside an IDisposable class. An
extremely simple example is a helper class that puts up a wait cursor
around a potentially long operation:

public class CursorWaiter : IDisposable
{
public CursorWaiter()
{
Cursor.Current = Cursors.WaitCursor;
}
~CursorWaiter()
{
Dispose(false);
}
public void Dispose()
{
Dispose(true);
}
protected void Dispose(bool disposing)
{
if (disposing)
{
GC.SuppressFinalize(this);
Cursor.Current = Cursors.Default;
}
}
}

// ...
using (new CursorWaiter())
{
// do slow stuff
}

You don't need a name for the CursorWaiter() instance because you
don't need to access it within the enclosed scope.
--
"The Direct3D Graphics Pipeline"-- code samples, sample chapter, FAQ:
<http://www.xmission.com/~legalize/book/>
Pilgrimage: Utah's annual demoparty
<http://pilgrimage.scene.org>
Nov 15 '05 #9

P: n/a
So, doing something like...

using (new CursorWaiter())
{
try
{}
catch
{}
}

right?

In this "extremely simple" example that doesn't really provide any
tremendous advantage over just changing the cursor in the try and finally
blocks, does it? I realize it could be more beneficial if that helper class
did more - I'm just trying to make sure I'm not missing something here.

"Rich" <le*************@mail.xmission.com> wrote in message
news:Oe**************@TK2MSFTNGP11.phx.gbl...
[Please do not mail me a copy of your followup]

Jon Skeet [C# MVP] <sk***@pobox.com> spake the secret code
<MP************************@msnews.microsoft.com > thusly:
That's just an example off the top of my head, and it's not terribly
good. I'm sure it's quite rare, but there are probably a few real cases
around :)


There are lots of examples where anonymous items in a using statement
are useful. Basically, any time you have to do something like this:

try
{
StartSomething();
// do stuff
}
finally
{
EndSomething();
}

You can encapsulate the Start/End inside an IDisposable class. An
extremely simple example is a helper class that puts up a wait cursor
around a potentially long operation:

public class CursorWaiter : IDisposable
{
public CursorWaiter()
{
Cursor.Current = Cursors.WaitCursor;
}
~CursorWaiter()
{
Dispose(false);
}
public void Dispose()
{
Dispose(true);
}
protected void Dispose(bool disposing)
{
if (disposing)
{
GC.SuppressFinalize(this);
Cursor.Current = Cursors.Default;
}
}
}

// ...
using (new CursorWaiter())
{
// do slow stuff
}

You don't need a name for the CursorWaiter() instance because you
don't need to access it within the enclosed scope.
--
"The Direct3D Graphics Pipeline"-- code samples, sample chapter, FAQ:
<http://www.xmission.com/~legalize/book/>
Pilgrimage: Utah's annual demoparty
<http://pilgrimage.scene.org>

Nov 15 '05 #10

P: n/a
[Please do not mail me a copy of your followup]

"Daniel Billingsley" <db**********@NO.durcon.SPAAMM.com> spake the secret code
<Of**************@TK2MSFTNGP12.phx.gbl> thusly:
So, doing something like...

using (new CursorWaiter())
{
try
{}
catch
{}
}

right?
Nope. The whole point of putting it in the using clause is that the
cursor will be restored no matter how you exit the enclosed block: a
shortcut return statement, an exception, or normal execution flow
through the last statement.
In this "extremely simple" example that doesn't really provide any
tremendous advantage over just changing the cursor in the try and finally
blocks, does it?


Yes, it does. You don't have to write the try/finally blocks if you
use the using statement, assuming that the only reason you wrote
try/finally in the first place was to restore the cursor when you
exited the "slow processing" step.
--
"The Direct3D Graphics Pipeline"-- code samples, sample chapter, FAQ:
<http://www.xmission.com/~legalize/book/>
Pilgrimage: Utah's annual demoparty
<http://pilgrimage.scene.org>
Nov 15 '05 #11

This discussion thread is closed

Replies have been disabled for this discussion.