Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/303.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/kotlin/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 了解列表(或集合)项的最有效方式&x27;谁的家长名单?_C#_List_Collections - Fatal编程技术网

C# 了解列表(或集合)项的最有效方式&x27;谁的家长名单?

C# 了解列表(或集合)项的最有效方式&x27;谁的家长名单?,c#,list,collections,C#,List,Collections,通常对于列表和其中的项目,我发现需要查询或查找对象添加到的列表中的内容 例如: // create list of levels IList<Levels> levels = new List<Levels> { levels.Add(new Level{ Depth = 1, Name = "Level 1" }); levels.Add(new Level{ Depth = 2, Name = "Level 2" }); } foreach(Leve

通常对于列表和其中的项目,我发现需要查询或查找对象添加到的列表中的内容

例如:

// create list of levels
IList<Levels> levels = new List<Levels>
{
    levels.Add(new Level{ Depth = 1, Name = "Level 1" });
    levels.Add(new Level{ Depth = 2, Name = "Level 2" });
}

foreach(Level level in levels)
{
    bool lowestLevel = false;

    // would the lowest level be best worked out from a function that takes the list and the level?
    lowestLevel = Level.GetLowest(Levels, level);

    // as a calculated property of level itself where the level knows about the list of levels it's in?
    lowestLevel = Level.IsLowest;

    // or worked out when creating the level?
    // levels.Add(new Level{ Depth = 1, Name = "Level 1", IsLowest = isLowest });
    lowestLevel = Level.IsLowest;
}
//创建级别列表
IList级别=新列表
{
添加(新级别{Depth=1,Name=“Level 1”});
添加(新级别{Depth=2,Name=“Level 2”});
}
foreach(级别中的级别)
{
bool lowestLevel=错误;
//从一个接受列表和级别的函数中计算出最低级别是最好的吗?
lowestLevel=Level.GetLowest(Levels,Level);
//作为级别本身的计算属性,级别知道它所在的级别列表?
lowestLevel=Level.IsLowest;
//还是在创建关卡时计算出来的?
//添加(新级别{Depth=1,Name=“级别1”,islowst=islowst});
lowestLevel=Level.IsLowest;
}
这些是处理类似问题的“最佳实践”方法吗?还是有其他方法

提前感谢。

为什么现在使用Linq:

lowest = list.Max(x => x.Level);
highest = list.Min(x => x.Level);
请注意,如果列表的类型为IList,则某些Linq方法不可用。此外,如果要获取实际对象(标高),则必须使用以下内容:

var lowest = list.OrderBy(x => x.Level).First();

忽略添加到正在迭代的集合会引发异常的事实


肯定还有另一种方法。当
级别
需要了解其同级时,应该将
级别
封装在类中,比如
级别集合
。在向集合中插入级别时,可以为每个
级别
提供对其父集合的引用,并停止在方法中传递
级别
,而不需要同级。最佳做法是为适当的任务选择适当的结构。列表是非常常见的结构,没有任何特殊功能

所以对于这种情况,您应该有一些结构,当您添加元素时,它应该决定将其放置在何处。整个图论将非常有助于您选择合适的解决方案。如果您发现这样的结构,那么您必须检查它是否已经实现。NET框架包含许多可能有用的通用结构

因此,您可以使用,而级别类应该实现接口

每当您将级别实例添加到这样的列表中时,最低级别将位于表的索引0下(或大小-1,这取决于您将使用的)


一篇关于的好文章不要在
级别
类型上存储
深度。而是传递对集合的引用。
Level.Parent=此
列表中。添加
方法。您总是可以通过List.IndexOf(Level)获得深度。这样,您就不必每次更改集合时都更新所有成员的深度

public class Level
{
    public IList<Level> Parent { get; set; }

    public string Name { get; set; }
}

public class LevelCollection : Collection<Level>
{
    protected override void InsertItem(int index, Level item)
    {
        base.InsertItem(index, item);
        item.Parent = this;
    }

    protected override void RemoveItem(int index)
    {
        this[index].Parent = null;
        base.RemoveItem(index);
    }
}

我直接在文本编辑器中写了这篇文章,为语法错误道歉。问题不清楚。您是否需要列表中的某个项目来“了解”包含该项目的列表,或者仅仅需要根据某些标准在列表中查找某个项目?此外,您所说的有效方式是什么意思?编码方面?性能?当使用级别列表时,级别之外的内容可能需要知道(例如在循环中)当前级别是否落在列表中的特定位置。对于这样的逻辑,您是否会询问级别(列表中的级别),或者从外部获取列表和级别,并保持逻辑独立?主要是性能,在项目中引用列表会是一种糟糕的做法吗?这是我想到的,但是最低级别(级别)是LevelCollection上封装linq结果的方法。我曾想过对每个级别的级别列表进行引用,但警钟开始响起。在这种情况下,您可以将对最低级别的引用存储为
LevelCollection
的一部分,并比较
IsLowest(level)
的引用。当您从
LevelCollection
中添加或删除
Level
时,您可以更新这些引用。@PhilCooper如果让
Level
对象引用其同级集合,
islowst
将成为
Level
的属性,该属性将调用
parent.getislowst(此)
获取答案。
this.Parent.IndexOf(this);