我有这个代码
Dim oCliente As New Net.WebClient()
Dim bHTML As Byte() = oCliente.DownloadData(Me.tbURL.Text)
Dim oUTF8 As New UTF8Encoding()
Me.tbShowArea.Text = oUTF8.GetString(bHTML)
和这个
Me.tbShowArea.Text = UTF8Encoding.UTF8.GetString(New Net.WebClient().DownloadData(Me.tbURL.Text))
他们俩都做同样的事情。我想知道的是,使用这两种方法的优缺点是什么?
基本上,唯一的区别是第二个函数链接了函数,因此每个函数都将获取前一个函数的结果并将其传递给下一个函数,而第一个函数将每个函数的结果分配给变量。
您的代码没有真正的区别,但是在其他情况下,您可能需要对同一结果执行更多处理,在这种情况下,您将使用变量。假设您想做WebClient
一件事。使用第一个,您可以执行以下操作:
Dim oCliente As New Net.WebClient()
Dim bHTML As Byte() = oCliente.DownloadData(Me.tbURL.Text)
Dim oUTF8 As New UTF8Encoding()
Me.tbShowArea.Text = oUTF8.GetString(bHTML)
bHTML = oCliente.DownloadData(Me.tbURL2.Text)
Me.tbShowArea2.Text = oUTF8.GetString(bHTML)
如您所见,我们重用了变量来执行其他任务。您不能用第二种方法来做,您将不得不重复整个过程:
Me.tbShowArea.Text = UTF8Encoding.UTF8.GetString(New Net.WebClient().DownloadData(Me.tbURL.Text))
Me.tbShowArea2.Text = UTF8Encoding.UTF8.GetString(New Net.WebClient().DownloadData(Me.tbURL2.Text))
现在假设您必须将其循环放置。您只能使用第一种方法。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句