Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
145 views
in Technique[技术] by (71.8m points)

c# - Using and Garbage Collection

Hi just to clairfy if I have the following:

using (Object1) {
create Object2
}
// bookmark1

Will Object2 be destroyed at bookmark1 along with Object1? Object2 is of StringReader and Object1 is of MemoryStream.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Neither object will be destroyed at the end of the block.

Object1 will be Disposed, a different concept; nothing will happen to Object2.

Both objects will be collected, and may be finalised, sometime later. Garbage collection is non-deterministic - you can't rely on when it will occur.

See IDisposable on MSDN for more.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...