正确使用“使用”声明 [英] Proper use of the "using" statement
问题描述
大家好,
我有一个SwitchCursor类,它实现了IDisposable。在使用
语句中使用未分配给引用的对象是否合法
如下:
using (新的SwitchCursor())//是来自GC的SwitchCursor安全吗?
{
}
或符合条件的对象对于GC?
提前致谢,
Andy
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
推荐答案
Andreas Müller< an *************** @ gmx.de>写道:
Andreas Müller <an***************@gmx.de> wrote:
我有一个类SwitchCursor,它实现了IDisposable。使用
使用(新的SwitchCursor())//是SwitchCursor的安全吗?来自GC?
或对象是否有资格获得GC?
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?
你不能这样做,因为using构造的语法要求
你将值赋给变量。
-
Jon Skeet - < sk *** @ pobox.com>
http:/ /www.pobox.com/~skeet
如果回复小组,请不要给我发邮件
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
Jon,
实际上,你可以做到这一点。根据C#规范的第8.13节(使用
语句),using关键字只需要有效的
表达式或局部变量声明。我认为C#
程序员的参考不正确。
以下代码:
使用(new System.Data.DataSet())
{
}
将编译。它还会创建一个隐含的变量声明,它存储在
中。这将使它不会被GC控制,直到使用块
退出。
希望这会有所帮助。
-
- Nicholas Paldino [.NET / C#MVP]
- < a href =mailto:mv*@spam.guard.caspershouse.com> mv*@spam.guard.caspershouse.com
" Jon Skeet [C# MVP] QUOT; < SK *** @ pobox.com>在消息中写道
新闻:MP ************************ @ msnews.microsoft.c om ...
AndreasMüller< an *************** @ gmx.de>写道:
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:
我有一个类SwitchCursor,它实现了IDisposable。使用
使用(新的SwitchCursor())//是SwitchCursor的安全吗?来自GC?
或对象是否有资格获得GC?
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?
你不能这样做,因为using构造的语法要求
你将值赋给变量。
-
Jon Skeet - < sk *** @ pobox.com>
http:/ /www.pobox.com/~skeet
如果回复小组,请不要给我发邮件
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
我相信SwitchCursor以这种方式使用是安全的。
Tu-Thach
I believe it is safe for SwitchCursor to be used this way.
Tu-Thach
-----原始信息-----
大家好,
我有一个SwitchCursor类,它实现了IDisposable。
使用未分配给类似于使用语句的引用
的对象是否合法:
使用(新的SwitchCursor())//是来自$的SwitchCursor安全b $ b GC?{
}
或对象是否有资格获得GC?
提前致谢,
Andy
。
-----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
.
这篇关于正确使用“使用”声明的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!