下面是我的代码:问题是当我将SortedList集合添加到Arraylist时,在检索时它(SortedList)转换为DictonaryEntry Strange。谁能解释一下?在此先感谢:)
public partial class WebForm1 : System.Web.UI.Page
{
ArrayList al = new ArrayList();
protected void Page_Load(object sender, EventArgs e)
{
al.Add('a');
al.Add(true);
al.Add(new ParentClass().MyProperty = 10);
al.Add("ABCD");
al.Add(3.00M);
ArrayList al1 = new ArrayList();
al1.Add("al1 array list");
al1.AddRange(al);
SortedList sl = new SortedList();
sl.Add(1, "sandeep");
al1.AddRange(sl); // Here I have added sorted list to Arraylist
foreach (object item in al1)
{
//---- The below code dont run properly (don't give desired output) , it gives output as System.Collections.DictionaryEntry
if (item is SortedList)
{
for (int i = 0; i < ((SortedList)item).Count; i++)
{
Response.Write(((SortedList)item).GetByIndex(i));
}
}
//// --- Below code run perfectly. and I get the desired output.
//if (item is DictionaryEntry)
// Response.Write(((DictionaryEntry)item).Value);
else
Response.Write(item);
Response.Write("<br/>");
}
}
}
答案 0 :(得分:0)
SortedList
不是列表 - 它是一个像列表一样的字典。它被列举为字典 - 按键值对。因此,当您al1.AddRange(s1);
添加组成排序列表的键值对时。
如果您想添加SortedList
的实际值,您应该al1.AddRange(s1.Values);
,但看到您想要添加SortedList
对象本身,您应该做的是{{ 1}}。