基本上,如何访问排序列表中的结构组件(键是字符串,值是结构)。该结构为节,其组件之一称为名称。我如何访问该组件。linkedList.GetByIndex(i).name
不工作。
发布于 2021-12-03 11:42:35
为了说明我的意见:
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
属性。
发布于 2021-12-03 06:16:11
据我所知,@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
}
}
UPDATE:插入"using System.Linq;“作为"ElementAt”是Linq的扩展方法
https://stackoverflow.com/questions/70214631
复制