从函数返回数组
本文关键字:数组 返回 函数 | 更新日期: 2023-09-27 18:02:04
我试图从这个函数返回一个数组。下面是函数:
public static string[] loadServers() {
WebClient client = new WebClient();
string getString = client.DownloadString("http://flippr.pw/en/web_service/servers.json");
client.Dispose();
Dictionary<string, Servers> listOfServers = JsonConvert.DeserializeObject<Dictionary<string, Servers>>(getString);
return listOfServers;
}
如何将数组返回到已声明的数组?
假设您想返回一个字符串数组,那么下面的示例显示了一个方法:
Dictionary<string, int> listOfServers = new Dictionary<string, int>();
listOfServers.Add("Server 1", 123980123);
listOfServers.Add("Server 2", 123234235);
string[] results = listOfServers.Select (x => x.Key).ToArray();
,其中listtifservers的int部分代表Servers
类型。在上面的例子中,它就是
return listOfServers.Select (x => x.Key).ToArray();
否则将方法的签名更改为Dictionary<string, Servers>
返回类型
你的返回类型是Dictionary,在你的函数中你指定返回类型为string[]。
public static Dictionary<string, Servers> loadServers() {
WebClient client = new WebClient();
string getString = client.DownloadString("http://flippr.pw/en/web_service/servers.json");
client.Dispose();
Dictionary<string, Servers> listOfServers = JsonConvert.DeserializeObject<Dictionary<string, Servers>>(getString);
return listOfServers;
}