使用有什么缺点Using
吗?我知道在Using
块之外我将无法使用该资源,但在某些情况下我应该将其留给垃圾收集器吗?
这是一些带有嵌套using
s 的示例代码。
Dim ftpReq As FtpWebRequest = Nothing
subSetupFtp(ftpReq, WebRequestMethods.Ftp.ListDirectory) 'Setup FTP
Dim lstFileNames As New List(Of String)
'Get FTP response
Using webRes As WebResponse = ftpReq.GetResponse()
'read filenames into list to return
Using ftpStream As New StreamReader(webRes.GetResponseStream())
Do While ftpStream.Peek <> -1
lstFileNames.Add(ftpStream.ReadLine)
Loop
lstFileNames.Sort() 'alphabetically sorts the list(a-z) ie. The files are now in date order
'Tidy up
ftpStream.Close()
webRes.Close()
End Using
End Using