我有以下代码:
public class Request
{
static string username = "ha@gmail.com";
public string Send()
{
///some variables
try
{
///
}
catch (WebException e)
{
using (WebResponse response = e.Response)
{
HttpWebResponse httpResponse = (HttpWebResponse)response;
Console.WriteLine("Error code: {0}", httpResponse.StatusCode);
using (Stream Data = response.GetResponseStream())
{
string text = new StreamReader(Data).ReadToEnd();
}
}
}
return text;
}
}
出现错误:“文本”在当前上下文中不存在。如何从方法返回“文本”值。
最佳答案
public string Send()
{
//define the variable outside of try catch
string text = null; //Define at method scope
///some variables
try
{
///
}
catch (WebException e)
{
using (WebResponse response = e.Response)
{
HttpWebResponse httpResponse = (HttpWebResponse)response;
Console.WriteLine("Error code: {0}", httpResponse.StatusCode);
using (Stream Data = response.GetResponseStream())
{
text = new StreamReader(Data).ReadToEnd();
}
}
}
return text;
}
关于c# - 从方法返回值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16606288/