2015-01-01 26 views
0

當我使用System.Net指令時,出現語法錯誤。我正在嘗試獲取網址的內容。下面是代碼:使用'using'指令時出現語法錯誤

using System.Net; 
WebClient client = new WebClient(); 
string reply = client.DownloadString (address); 
Console.WriteLine (reply); 

錯誤我在網上看到1:Syntax error, '(' expected

回答

0

using System.Net;命名空間上方編輯器的最頂端。你在你的方法體的範圍內。

只有using statement去那裏你的範圍是不同的。

在這種情況下,WebClient使用IDisposable,您應該使用using語句。例如:

using (WebClient client = new WebClient()) 
{ 
    string reply = client.DownloadString (address); 
    Console.WriteLine (reply); 
} 
相關問題