gpt4 book ai didi

c# - 从方法返回值

转载 作者:行者123 更新时间:2023-11-30 19:42:52 25 4
gpt4 key购买 nike

我有以下代码:

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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com