使用关键字 [英] Using keyword

查看:99
本文介绍了使用关键字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这里的任何人都可以确认reader.Close()和Dispose()将使用以下例程调用



using(SqlDataReader) reader = GetOpenReaderFunction())

{

while(reader.Read())doSomething(reader);

}


我在这里和那里看到了各种假设的例子。

Can anyone here confirm for sure that reader.Close() and Dispose() will
get called using the following routine:

using(SqlDataReader reader = GetOpenReaderFunction())
{
while(reader.Read()) doSomething(reader);
}

I have seen examples here and there with mixed assumptions.

推荐答案

INeedADip,


是的,肯定会关闭。对于所有意图和目的,C#

编译器将其转换为:

SqlDataReader reader = GetOpenReaderFunction();


尝试

{

而(reader.Read())doSomething(读者);

}

最后

{

如果(读者!= null)

{

((IDispose)读者).Dispose ();

}

}


希望这会有所帮助。

-

- Nicholas Paldino [.NET / C#MVP]

- mv*@spam.guard。 caspershouse.com

" INeedADip" <在******* @ gmail.com>在消息中写道

news:11 ********************** @ g47g2000cwa.googlegr oups.com ...
INeedADip,

Yes, it will definitely close. For all intents and purposes, the C#
compiler translates that into:

SqlDataReader reader = GetOpenReaderFunction();

try
{
while(reader.Read()) doSomething(reader);
}
finally
{
if (reader != null)
{
((IDispose) reader).Dispose();
}
}

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com
"INeedADip" <in*******@gmail.com> wrote in message
news:11**********************@g47g2000cwa.googlegr oups.com...
这里的任何人都可以确认使用以下例程调用reader.Close()和Dispose():

使用(SqlDataReader reader = GetOpenReaderFunction())
{
while(reader.Read())doSomething(reader);


我已经看到了各种各样的例子。
Can anyone here confirm for sure that reader.Close() and Dispose() will
get called using the following routine:

using(SqlDataReader reader = GetOpenReaderFunction())
{
while(reader.Read()) doSomething(reader);
}

I have seen examples here and there with mixed assumptions.



当使用

语句下的范围退出时,将自动调用Dispose。


和Dispose将调用Close()。


" INeedADip" <在******* @ gmail.com>在消息中写道

news:11 ********************** @ g47g2000cwa.googlegr oups.com ...
Dispose will be called automatically when the scope under the using
statement exits.

And Dispose will call Close().

"INeedADip" <in*******@gmail.com> wrote in message
news:11**********************@g47g2000cwa.googlegr oups.com...
这里的任何人都可以确认使用以下例程调用reader.Close()和Dispose():

使用(SqlDataReader reader = GetOpenReaderFunction())
{
while(reader.Read())doSomething(reader);


我已经看到了各种各样的例子。
Can anyone here confirm for sure that reader.Close() and Dispose() will
get called using the following routine:

using(SqlDataReader reader = GetOpenReaderFunction())
{
while(reader.Read()) doSomething(reader);
}

I have seen examples here and there with mixed assumptions.



INeedADip写道:
INeedADip wrote:
这里的任何人都可以确认reader.Close()和Dispose()将被调用使用以下例程:

使用(SqlDataReader reader = GetOpenReaderFunction())
{while /(reader.Read())doSomething(reader);
}

我在这里和那里看到了混合假设的例子。
Can anyone here confirm for sure that reader.Close() and Dispose() will
get called using the following routine:

using(SqlDataReader reader = GetOpenReaderFunction())
{
while(reader.Read()) doSomething(reader);
}

I have seen examples here and there with mixed assumptions.




当退出使用块时,将调用Dispose,无论是通过

正常的代码执行或如果发生异常。

的Dispose方法SqlDataReader调用Close以确保连接已关闭。


作为通用语句,使用保证将调用Dispose。它是
取决于Dispose的实现,确保资源正确释放



-

Tom波特菲尔德



Dispose will be called when the using block is exited, whether through
normal code execution or if an exception occurs. The Dispose method of
SqlDataReader calls Close to insure that the connection has been closed.

As a generic statement, using insures that Dispose will be called. It
is up to the implementation of Dispose that insures resources are
properly released.
--
Tom Porterfield


这篇关于使用关键字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆