C# 嵌套分类列表

C# 嵌套分类列表,c#,C#,我是C语言的新手,有PHP的背景,多维数组是一种闲逛 如何使用C中的SortedList创建嵌套/多维数组?通过阅读,我相信SortedList在PHP功能方面可能是最具可比性的。我尝试了以下操作,但它会抛出错误: SortedList arr = new SortedList(); arr.Add("hello",new SortedList()); arr.hello.Add("world",new SortedList()); 上课 public class MyList

我是C语言的新手,有PHP的背景,多维数组是一种闲逛

如何使用C中的SortedList创建嵌套/多维数组?通过阅读,我相信SortedList在PHP功能方面可能是最具可比性的。我尝试了以下操作,但它会抛出错误:

SortedList arr = new SortedList();
arr.Add("hello",new SortedList());
arr.hello.Add("world",new SortedList());
上课

    public class MyList
    {
        string name { get; set;}
        List<MyList> children { get; set; }
    }

这个怎么样

class NestedSortedList<T> : SortedList<T, NestedSortedList<T>> { }
而输出

但请注意。由于非泛型SortedList接收类型对象作为键和值,这意味着您可以存储您想要的任何对象,但您始终需要正确地转换它们,以便使用它们作为其原始类型


希望这对您有所帮助

您可能需要使用专业提示:c是一种静态类型语言。您的示例只能在非类型化语言(如PHP或JavaScript)上工作,或者通过使用dynamicData结构,这在强类型语言中是一个更大的主题。如果您能更清楚地了解您的确切要求,我们可以为您指引正确的方向。几乎可以肯定,您应该使用泛型。如今,非通用分类列表有点淡紫色的十年氛围。@CamiloTerevinto您不需要非类型语言或动态。您只需要使用非泛型集合,或者使用object作为泛型参数。当然,还有更好的方法可以使用这样的模型来维护静态类型。如果您想使用arr.hello.world,您可以使用动态或创建类来提供这种结构。谢谢!如果不先创建嵌套类,您就不能执行此操作吗?我已经编辑了答案,以演示如何在不创建嵌套类的情况下执行此操作-我认为我不能在此处添加字符串以及列表作为第二个参数?
class NestedSortedList<T> : SortedList<T, NestedSortedList<T>> { }
internal class Program
    {
        private static void Main(string[] args)
        {
            var nestedSortedList = new NestedSortedList<string>();
            nestedSortedList.Add("1", new NestedSortedList<string>());
            nestedSortedList.Add("2", new NestedSortedList<string>());
            nestedSortedList.Add("3", new NestedSortedList<string>());

            nestedSortedList["1"].Add("11", new NestedSortedList<string>());
            nestedSortedList["2"].Add("21", new NestedSortedList<string>());
            nestedSortedList["2"].Add("22", new NestedSortedList<string>());

            foreach (var item in nestedSortedList)
            {
                Console.WriteLine(item);
                foreach (var value in item.Value.Values)
                {
                    Console.WriteLine(value);
                }
                Console.WriteLine();
            }

            Console.ReadLine();
        }
}
internal class Program
    {
        private static void Main(string[] args)
        {
            var sortedList = new SortedList();
            sortedList.Add("1", new SortedList());
            sortedList.Add("2", new SortedList());
            sortedList.Add("3", new SortedList());

            ((SortedList)sortedList["1"]).Add("11", new SortedList());
            ((SortedList)sortedList["2"]).Add("21", new SortedList());
            ((SortedList)sortedList["2"]).Add("22", new SortedList());

            foreach (DictionaryEntry dictionaryEntry in sortedList)
            {
                Console.WriteLine("Key: {0}, Value: {1}",dictionaryEntry.Key,dictionaryEntry.Value);
                foreach (DictionaryEntry innerDictionaryEntry in (SortedList)dictionaryEntry.Value)
                {
                    Console.WriteLine("Inner >>> Key: {0}, Value: {1}", innerDictionaryEntry.Key,
                        innerDictionaryEntry.Value);
                }
                Console.WriteLine();
            }

            Console.ReadLine();
        }
}