基本上如何访问排序列表中结构的组件(键是字符串,值是结构(。该结构名为Section,其中一个组成部分名为name。如何访问该组件。linkedList.GetByIndex(i).name
不起作用。
为了说明我的评论:
struct A
{
public string name;
}
static void Demo()
{
// using System.Collections.Generic;
SortedList<string, A> list = new SortedList<string, A>();
list.Add("k0", new A { name = "name0" });
var name0 = list.Values[0].name;
}
要访问示例列表中的第一个结构,请使用list.Values[0]
或list["k0"]
。然后可以访问结构的.name
属性。
据我所知,@AxelKemper的帖子应该会有所帮助。
一个简单的例子:
public struct Section
{
public string Name { get; set; }
public SortedList<string, string> List { get; set; }
}
public class SectionsClass
{
public string Indentifier { get; set; }
public SortedList<string, Section> Sections { get; set; }
}
public class MyTestClass
{
public MyTestClass()
{
var sc = new SectionsClass
{
Indentifier = "A",
Sections = new SortedList<string, Section> {
{"1", new Section { Name = "AA" } },
{"2", new Section { Name = "AB" } },
}
};
Debug.WriteLine(sc.Sections.ElementAt(0).Value.Name); // AA
}
}
更新:插入";使用System.Linq"作为";ElementAt";是Linq 的扩展方法