如何避免“选择”后容器中的物品顺序发生变化扩展方法的使用?

发布于 2024-10-15 23:53:04 字数 857 浏览 2 评论 0原文

这是一段代码:

public class Schema
{
    public Schema(List<CountryParticipantsStages> places)
    {
        Places = places.Select(participant =>(AbstractGeneralParticipants) new GeneralParticipants(participant)).ToList();
    }
...

如果源列表被完整迭代,源列表和结果列表中的元素顺序将相同。但是,如果在任何迭代中间调用“Schema”构造函数,那么“Places”列表中的元素顺序将发生变化......

为了避免这种情况,我看到唯一的方法是不使用“Select”方法,而是使用“for” ' 将从第 0 个元素开始的循环:

public class Schema
{
    public Schema(List<CountryParticipantsStages> places)
    {
        Places = new List<AbstractGeneralParticipants>(places.Count);
        for (int i = 0; i < places.Count; i++)
        {
            Places.Add(new GeneralParticipants(places[i]));
        }
    }

第二个函数看起来令人不快,但我没有看到更好的方法。而且,ReSharper 建议我用“foreach”重放“for”循环...

请指教。

多谢。

Here is a piece of code:

public class Schema
{
    public Schema(List<CountryParticipantsStages> places)
    {
        Places = places.Select(participant =>(AbstractGeneralParticipants) new GeneralParticipants(participant)).ToList();
    }
...

Order of element in source and result lists will be the same if source list is iterated in full. But if if 'Schema' constructor is called in the middle of any iteration then order of elements in 'Places' list will be shifted...

To avoid this I see the only way to use not a 'Select' method but a 'for' loop that will go from 0th element:

public class Schema
{
    public Schema(List<CountryParticipantsStages> places)
    {
        Places = new List<AbstractGeneralParticipants>(places.Count);
        for (int i = 0; i < places.Count; i++)
        {
            Places.Add(new GeneralParticipants(places[i]));
        }
    }

The 2nd function looks unpleasantly, but I don't see any better way for that. And also, ReSharper suggests me to replays 'for' loop with 'foreach'...

Please advise.

Thanks a lot.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

深爱不及久伴 2024-10-22 23:53:04

您错误地认为,如果列表在程序的某个地方被迭代,那么当它作为参数传递时,它的当前位置就会发生变化。

该列表不包含任何当前索引或状态。这是客户端代码的作用。

foreach 本质上是调用 GetEnumerator 方法并使用 IEnumerator 向前移动并读取当前值。一旦您将 list 传递到其他地方,另一个 foreach 就会获得自己的 IEnumerator ,它再次从第一个元素开始。

考虑这个示例:

void InnerEnumerate (List<int> innerList)
{
    foreach (var innerItem in innerList)
        Console.WriteLine ("- inner item {0}", innerItem);
}

var outerList = new List<int> { 1, 2, 3 };
foreach (var outerItem in outerList) {
    Console.WriteLine ("Outer item {0}", outerItem);
    InnerEnumerate (outerList); // pass list as a parameter
}

这是输出:

Outer item 1
- inner item 1
- inner item 2
- inner item 3
Outer item 2
- inner item 1
- inner item 2
- inner item 3
Outer item 3
- inner item 1
- inner item 2
- inner item 3

正如您所看到的,foreach 是独立的并且彼此不了解任何信息。他们总是从头开始。

You're wrong to think that if list is being iterated in some place of program, its current position becomes shifted when it is passed as a parameter.

The list does not hold any current index or state. It's the client code that does.

What foreach essentially does is calling GetEnumerator method and uses IEnumerator to move forward and read current value. Once you pass list somewhere else, another foreach gets its own IEnumerator which starts from first element again.

Consider this sample:

void InnerEnumerate (List<int> innerList)
{
    foreach (var innerItem in innerList)
        Console.WriteLine ("- inner item {0}", innerItem);
}

var outerList = new List<int> { 1, 2, 3 };
foreach (var outerItem in outerList) {
    Console.WriteLine ("Outer item {0}", outerItem);
    InnerEnumerate (outerList); // pass list as a parameter
}

This is the output:

Outer item 1
- inner item 1
- inner item 2
- inner item 3
Outer item 2
- inner item 1
- inner item 2
- inner item 3
Outer item 3
- inner item 1
- inner item 2
- inner item 3

As you can see, foreach es are independent and don't know anything about each other. They always start from the beginning.

平安喜乐 2024-10-22 23:53:04

我猜,最好的解决方案是避免依赖列表中的项目顺序。相反,最好在“CountryParticipantsStages”类中添加额外的参数,该参数将用于确定其在列表中的“位置”。

Guess, the best solution will be to avoid relying on the items order in the list. Instead would be better to add additional parameter into 'CountryParticipantsStages' class that will be used to determining its 'position' in list.

顾冷 2024-10-22 23:53:04

在第二个示例中,您可以按照 Resharper 的建议使用 foreach 循环:

Places = new List<AbstractGeneralParticipants>(places.Count);
foreach (Place place in places)
{
    Places.Add(new GeneralParticipants(place));
}

或者您也可以像第一个示例中那样使用 LINQ 来执行此操作,这更简洁一些。

In your second example you can use a foreach loop as Resharper suggested:

Places = new List<AbstractGeneralParticipants>(places.Count);
foreach (Place place in places)
{
    Places.Add(new GeneralParticipants(place));
}

Or you can also do it using LINQ as in your first example, which is a bit more concise.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文