如何避免“选择”后容器中的物品顺序发生变化扩展方法的使用?
这是一段代码:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您错误地认为,如果列表在程序的某个地方被迭代,那么当它作为参数传递时,它的当前位置就会发生变化。
该列表不包含任何当前索引或状态。这是客户端代码的作用。
foreach
本质上是调用GetEnumerator
方法并使用IEnumerator
向前移动并读取当前值。一旦您将 list 传递到其他地方,另一个foreach
就会获得自己的IEnumerator
,它再次从第一个元素开始。考虑这个示例:
这是输出:
正如您所看到的,
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 callingGetEnumerator
method and usesIEnumerator
to move forward and read current value. Once you pass list somewhere else, anotherforeach
gets its ownIEnumerator
which starts from first element again.Consider this sample:
This is the output:
As you can see,
foreach
es are independent and don't know anything about each other. They always start from the beginning.我猜,最好的解决方案是避免依赖列表中的项目顺序。相反,最好在“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.
在第二个示例中,您可以按照 Resharper 的建议使用 foreach 循环:
或者您也可以像第一个示例中那样使用 LINQ 来执行此操作,这更简洁一些。
In your second example you can use a foreach loop as Resharper suggested:
Or you can also do it using LINQ as in your first example, which is a bit more concise.