Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I use a StringReader to turn a string into something I can upload to an SFTP server (it takes a stream). Is there any point in closing that StringReader afterwards? As far as I can see in the source it just sets the string to null...

I could just do it, but since the close method is marked as throwing an IOException and all I have to wrap it in a try catch and the code just ends up looking a lot more horrible than it perhaps needs to be.

See Question&Answers more detail:os

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

1 Answer

If you know you're dealing with a StringReader that you'll be throwing away, I don't see any reason to close it. I can't imagine any reason you'd be holding a reference to it after you'd close it, so there's no real benefit to the string being set to null for garbage collection. If you were creating a method that takes a Reader then it might make sense to close it since you don't know the underlying type.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...