C#中数组的键值对列表
本文关键字:键值对 列表 数组 | 更新日期: 2023-09-27 18:22:41
我正在开发Silverlight应用程序。由于SL不支持arraylist,所以我只能使用Arrays和List of object。我想将key-valuepair的列表转换为数组。但当我做以下事情时:
private KeyValuePair<String, int>[] array1;
List<KeyValuePair<String, int>> list1 = methodCall.Result();
array1 = list1.ToArray();
我已经调试并确认,由于方法调用,list1的结果不是空的。但是,即使在转换之后,array1也是空的。我做错了什么?
编辑:以下是代码隐藏的完整代码。
private KeyValuePair<String, int>[] array1;
private KeyValuePair<string, int>[] getlocalUniversities()
{
ASASService.ASASServiceClient client1 = new ASASService.ASASServiceClient();
client1.getLocalUniversitiesCompleted += new EventHandler<ASASService.getLocalUniversitiesCompletedEventArgs>(client_getLocalUniversitiesCompleted);
client1.getLocalUniversitiesAsync();
return array1;
}
void client_getLocalUniversitiesCompleted(object sender, ASASService.getLocalUniversitiesCompletedEventArgs ex)
{
if (ex.Error == null && ex.Result != null)
{
List<KeyValuePair<string, int>> list1 = ex.Result;
array1 = list1.ToArray();
}
else
{
MessageBox.Show(ex.Error.Message);
}
}
//
THE ASASService method getLocalUniversities() returns a List<KeyValuePair<String, int>>.
From there, I see that it has 1(expected) result consisting of <"NUS", 50>.
However, when I get it here as ex.Result, ex.Result contains 1 result consisting of <null, 0>.
我认为您在调试时可能犯了一个错误。假设Result()方法运行良好,那么您列出的代码应该已经运行了。我把它作为一个测试来运行,它运行得很好。
KeyValuePair<string, int>[] array1;
List<KeyValuePair<string, int>> list1 = new List<KeyValuePair<string, int>>();
list1.Add(new KeyValuePair<string, int>("one", 1));
list1.Add(new KeyValuePair<string, int>("two", 2));
list1.Add(new KeyValuePair<string, int>("three", 3));
array1 = list1.ToArray();