使用,最后声明 [英] using and finally statement

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

问题描述

我是否还需要在catch之后关闭finally语句中的对象如果

它是using语句的一部分?


例如:

FileStream fs = null;


尝试

{

using(FileInfo fInfo = new FileInfo (fromImagePath +" \\" +

(string)dr [" originalFileName"]))

{

if (fInfo.Exists)

{

numBytes = fInfo.Length;


//将文件读入字节数组


使用(fs = new FileStream(fromImagePath +" \\" +

(string)dr [" originalFileName"],FileMode。打开,System.IO.FileAccess.Read))

{

使用(BinaryReader br = new BinaryReader(fs))

myByteArray = br .ReadBytes((int)numBytes);

}


//用新名称将文件写入images文件夹

使用(fs = System.IO.File.OpenWrite(imagePath + imageName))

{

fs.Write(myByteArray,0,myByteArray.Length);

}

}

} < br $>
}

catch(例外情况)

{

}

终于

{

if(br!= null)

br.Close();

if(fs!= null )

fs.Close();

}


我是否需要使用finally代码或执行使用 ;自动关闭

对象(fs和br)?


此外,当你退出using语句时(有​​或没有错误 -

会将你发送给catch子句,在using语句之外定义的对象是否为null?


谢谢,


Tom

解决方案

tshad< ts *** @dslextreme.comwrote:


如果

它是using语句的一部分,我还需要在catch之后关闭finally语句中的对象吗?



否。使用声明的重点是自动完成。


我倾向于只声明每个使用语句中的FileStream fs,

而不是共享一个变量用于两次使用。


(注意FileInfo没有实现IDisposable,所以你的代码

目前实际上并不编译。)


另外,当你退出using语句时(有​​或没有错误) -

会将你发送到catch子句),在using语句之外定义的对象是否为null?



不,它会有相同的价值,只是对象本来是处置的,因此不是非常有用。这是

在using语句本身中声明变量的一个原因 - 它会阻止你尝试使用已经被处置的对象的



-

Jon Skeet - < sk *** @ pobox.com>

网站: http://www.pobox.com/~skeet

博客:< a rel =nofollowhref =http://www.msmvps.com/jon.skeettarget =_ blank> http://www.msmvps.com/jon.skeet

C#深度: http://csharpindepth.com




" Jon Skeet [C#MVP]" < sk *** @ pobox.com写了留言

新闻:MP ********************* @ msnews.microsoft.com 。 ..


tshad< ts *** @dslextreme.comwrote:


>我是否在捕获之后仍然需要关闭finally语句中的对象
如果它是using语句的一部分吗?



否。使用声明的重点是自动完成。


我倾向于只声明每个使用语句中的FileStream fs,

而不是共享一个变量用于两次使用。


(注意FileInfo没有实现IDisposable,所以你的代码

目前实际上并不编译。)


>此外,当你退出使用声明时(有或没有)一个错误 -
会将你发送到catch子句),在using语句之外定义的对象是否为null?



不,它会有相同的价值,只是对象本来会被处理掉,因此不是非常有用。这是

在using语句本身中声明变量的一个原因 - 它会阻止你尝试使用已经被处置的对象的



有道理。


以下内容:


using(fs = new FileStream (fromImagePath +" \\" +

(string)dr [" originalFileName"],FileMode.Open,System.IO.FileAccess.Read))

{

使用(BinaryReader br = new BinaryReader(fs))

myByteArray = br.ReadBytes((int)numBytes);

}


将二进制读取器放在using语句中有点矫枉过正吗?


谢谢,


Tom


-

Jon Skeet - < sk *** @ pobox.com>

网站: http://www.pobox.com/~skeet

博客: http://www.msmvps.com/jon.skeet

C#深度: http://csharpindepth.com



tshad< ts *** @ dslextreme.comwrote:


有道理。


以下内容:


using(fs = new FileStream(fromImagePath +" \\" +

(字符串)dr [" originalFileName"],FileMode.Open,System.IO.FileAccess.Read))

{

using(BinaryReader br = new BinaryReader(fs))

myByteArray = br.ReadBytes((int)numBytes);

}


将二进制读取器放在using语句中有点矫枉过正吗?



是的,不过我倾向于这样做 - 最好养成

总是处理东西的习惯,并且可以存在这样的包装案例

其中*确实*有所作为(因为关闭内部资源

可以刷新对外部资源的挂起写入)。 />

-

Jon Skeet - < sk *** @ pobox.com>

网站: http://www.pobox.com/~skeet

博客: http://www.msmvps.com/jon.skeet

C#深度: http://csharpindepth.com


Do I still need to close an object in a finally statement after a catch if
it is part of a using statement?

For example:
FileStream fs = null;

try
{
using (FileInfo fInfo = new FileInfo(fromImagePath + "\\" +
(string)dr["originalFileName"]))
{
if (fInfo.Exists)
{
numBytes = fInfo.Length;

// Read the file into the byte array

using (fs = new FileStream(fromImagePath + "\\" +
(string)dr["originalFileName"], FileMode.Open, System.IO.FileAccess.Read))
{
using(BinaryReader br = new BinaryReader(fs))
myByteArray = br.ReadBytes((int)numBytes);
}

// Write out the file into the images folder with new name

using (fs = System.IO.File.OpenWrite(imagePath + imageName))
{
fs.Write(myByteArray, 0, myByteArray.Length);
}
}
}
}
catch (Exception exc)
{
}
finally
{
if (br != null)
br.Close();
if (fs != null)
fs.Close();
}

Do I need to use the finally code or does the "using" automatically close
the objects (fs and br)?

Also, when the you exit the using statement (with or without an error -
which would send you to the catch clause), would the an object that was
defined outside of the using statement be null?

Thanks,

Tom

解决方案

tshad <ts***@dslextreme.comwrote:

Do I still need to close an object in a finally statement after a catch if
it is part of a using statement?

No. The whole point of a using statement is to do that automatically.

I would tend to only declare the FileStream fs in each using statement,
rather than sharing one variable for two uses.

(Note that FileInfo doesn''t implement IDisposable, so your code
wouldn''t actually compile at the moment.)

Also, when the you exit the using statement (with or without an error -
which would send you to the catch clause), would the an object that was
defined outside of the using statement be null?

No, it would have the same value, it''s just the object would have been
disposed and therefore not terribly useful. That''s one reason to
declare the variable in the using statement itself - it stops you from
trying to use an object which has already been disposed.

--
Jon Skeet - <sk***@pobox.com>
Web site: http://www.pobox.com/~skeet
Blog: http://www.msmvps.com/jon.skeet
C# in Depth: http://csharpindepth.com



"Jon Skeet [C# MVP]" <sk***@pobox.comwrote in message
news:MP*********************@msnews.microsoft.com. ..

tshad <ts***@dslextreme.comwrote:

>Do I still need to close an object in a finally statement after a catch
if
it is part of a using statement?


No. The whole point of a using statement is to do that automatically.

I would tend to only declare the FileStream fs in each using statement,
rather than sharing one variable for two uses.

(Note that FileInfo doesn''t implement IDisposable, so your code
wouldn''t actually compile at the moment.)

>Also, when the you exit the using statement (with or without an error -
which would send you to the catch clause), would the an object that was
defined outside of the using statement be null?


No, it would have the same value, it''s just the object would have been
disposed and therefore not terribly useful. That''s one reason to
declare the variable in the using statement itself - it stops you from
trying to use an object which has already been disposed.

Makes sense.

In the following:

using (fs = new FileStream(fromImagePath + "\\" +
(string)dr["originalFileName"], FileMode.Open, System.IO.FileAccess.Read))
{
using(BinaryReader br = new BinaryReader(fs))
myByteArray = br.ReadBytes((int)numBytes);
}

Is putting the BinaryReader in the using statement a little overkill?

Thanks,

Tom

--
Jon Skeet - <sk***@pobox.com>
Web site: http://www.pobox.com/~skeet
Blog: http://www.msmvps.com/jon.skeet
C# in Depth: http://csharpindepth.com



tshad <ts***@dslextreme.comwrote:

Makes sense.

In the following:

using (fs = new FileStream(fromImagePath + "\\" +
(string)dr["originalFileName"], FileMode.Open, System.IO.FileAccess.Read))
{
using(BinaryReader br = new BinaryReader(fs))
myByteArray = br.ReadBytes((int)numBytes);
}

Is putting the BinaryReader in the using statement a little overkill?

Yes, but I tend to do it anyway - it''s better to get into the habit of
always disposing of things, and there can be wrapping cases like this
where it *does* make a difference (because closing the inner resource
may flush pending writes to the outer resource).

--
Jon Skeet - <sk***@pobox.com>
Web site: http://www.pobox.com/~skeet
Blog: http://www.msmvps.com/jon.skeet
C# in Depth: http://csharpindepth.com


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

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