gpt4 book ai didi

c# - foreach 循环中奇怪的 IEnumerable 行为

转载 作者:行者123 更新时间:2023-11-30 19:11:28 24 4
gpt4 key购买 nike

我有以下方法:

public IEnumerable<object> GetParameters(Context context)
{
yield return new[] { 1, 2, 3 };
}

当我循环遍历此枚举时:

foreach (var parameter in GetParameters())
{
// Do something here with parameter
}

我希望参数在第一个循环中为 1,在第二个循环中为 2,在第三个循环中为 3,但这不是案例:(

enter image description here

参数是一个包含所有值的数组!

我在这里错过了什么??

最佳答案

您将返回可枚举数组的单个实例。

你想要:

yield return 1;
yield return 2;
yield return 3;

关于c# - foreach 循环中奇怪的 IEnumerable 行为,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11436391/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com