gpt4 book ai didi

c# - 捕获的变量 IndexOutOfBounds

转载 作者:太空宇宙 更新时间:2023-11-03 22:28:52 26 4
gpt4 key购买 nike

<分区>

我有两个代码循环片段,后者按预期工作,而前者抛出异常。为什么 foreach 有效而 for 循环无效?它源于什么?

IEnumerable<char> query = "Not what you might expect";

query = query.Where (c => c != 'a');
query = query.Where (c => c != 'e');
query = query.Where (c => c != 'i');
query = query.Where (c => c != 'o');
query = query.Where (c => c != 'u');

foreach (char c in query) Console.Write (c); // Nt wht y mght xpct

有异常的for循环

IEnumerable<char> query = "Not what you might expect";
string vowels = "aeiou";

for (int i = 0; i < vowels.Length; i++)
query = query.Where (c => c != vowels[i]);

foreach (char c in query) Console.Write (c);

foreach代码,

foreach (char vowel in vowels)
query = query.Where (c => c != vowel);

foreach (char c in query) Console.Write (c);

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