Counter in foreach loop in C#

Working of foreach: As I know, foreach is a loop which iterates through a collection or array one by one, starting from 0 index till the last item of the collection. So, if I have n items in an array. foreach (var item in arr) { } then, In, 1st iteration, item=arr[0]; then, in 2nd, item=arr[1]; . . . in last (nth), item=arr[n-1]; Conclusion: from working it seems that at each

在C#中的foreach循环中计数器

foreach的工作:据我所知, foreach是一个循环,它从0索引开始一直到集合的最后一个项目,逐个遍历集合或数组。 所以,如果我有一个数组中的n个项目。 foreach (var item in arr) { } 然后,在第一次迭代中, item = arr [0]; 那么,在第二项中, item = arr [1]; 。 。 。 在last(nth)中, item = arr [n-1]; 结论:从工作中看来,在每次迭代中它都知道从数组中获取哪个值,或者知道要从数组中获取的项

Foreach loop, determine which is the last iteration of the loop

I have a foreach loop and need to execute some logic when the last item is chosen from the List , eg: foreach (Item result in Model.Results) { //if current result is the last item in Model.Results //then do something in the code } Can I know which loop is last without using for loop and counters? If you just need to do something with the last element (as opposed to something di

Foreach循环,确定哪个是循环的最后一次迭代

我有一个foreach循环,当从List选择最后一个项目时需要执行一些逻辑,例如: foreach (Item result in Model.Results) { //if current result is the last item in Model.Results //then do something in the code } 我可以知道哪个循环是最后一个循环而不使用循环和计数器吗? 如果你只需要用最后一个元素做一些事情(与最后一个元素不同的地方) ,那么使用LINQ将在这里帮助: Item last = Model.Results.L

Get index of Enumerator.Current in C#

Possible Duplicate: (C#) Get index of current foreach iteration Good morning, Is there any way I can get the index of an Enumerator 's current element (in the case, a character in a string) without using an ancillary variable? I know this would perhaps be easier if I used a while or for cicle, but looping through a string using an enumerator is more elegant... The only drawback for the

在C#中获取Enumerator.Current的索引

可能重复: (C#)获取当前foreach迭代的索引 早上好, 有什么办法可以在不使用辅助变量的情况下获取Enumerator的当前元素的索引(在这种情况下,字符串中的字符)? 我知道这或许会是,如果我用一个更容易while还是for cicle,而是通过使用枚举的字符串循环是更优雅...的情况下,唯一的缺点是,我真的需要每个人物的当前索引。 非常感谢你。 不, IEnumerator接口不支持这种功能。 如果你需要这个,你可以自己实

Get current index from foreach loop

This question already has an answer here: How do you get the index of the current iteration of a foreach loop? 31 answers IEnumerable list = DataGridDetail.ItemsSource as IEnumerable; List<string> lstFile = new List<string>(); int i = 0; foreach (var row in list) { bool IsChecked = (bool)((CheckBox)DataGridDetail.Columns[0].GetCellContent(row)).IsChecked; if (IsChecked) { Messa

从foreach循环获取当前索引

这个问题在这里已经有了答案: 你如何获得foreach循环的当前迭代的索引? 31个答案 IEnumerable list = DataGridDetail.ItemsSource as IEnumerable; List<string> lstFile = new List<string>(); int i = 0; foreach (var row in list) { bool IsChecked = (bool)((CheckBox)DataGridDetail.Columns[0].GetCellContent(row)).IsChecked; if (IsChecked) { MessageBox.show(i); --Here i want to get the inde

foreach with index

This question already has an answer here: How do you get the index of the current iteration of a foreach loop? 31 answers I keep this extension method around for this: public static void Each<T>( this IEnumerable<T> ie, Action<T, int> action ) { var i = 0; foreach ( var e in ie ) action( e, i++ ); } And use it like so: var strings = new List<string>(); stri

带索引的foreach

这个问题在这里已经有了答案: 你如何获得foreach循环的当前迭代的索引? 31个答案 我为此保留了这个扩展方法: public static void Each<T>( this IEnumerable<T> ie, Action<T, int> action ) { var i = 0; foreach ( var e in ie ) action( e, i++ ); } 像这样使用它: var strings = new List<string>(); strings.Each( ( str, n ) => { // hooray } ); 或者考虑到类似brea

Existing LINQ extension method similar to Parallel.For?

Possible Duplicate: LINQ equivalent of foreach for IEnumerable<T> The linq extension methods for ienumerable are very handy ... but not that useful if all you want to do is apply some computation to each item in the enumeration without returning anything. So I was wondering if perhaps I was just missing the right method, or if it truly doesn't exist as I'd rather use a built-in

现有的LINQ扩展方法类似于Parallel.For?

可能重复: IEnumerable <T>的foreach相当于LINQ 可枚举的linq扩展方法非常方便,但如果您只想对枚举中的每个项目应用一些计算而不返回任何内容,那么这种方法就没有用处。 所以我想知道如果也许我只是错过了正确的方法,或者如果它真的不存在,我宁愿使用内置版本,如果它可用...但我还没有找到一个:-) 我可以发誓在那里有一个.ForEach方法,但我还没有找到它。 与此同时,我写了自己的版本,以防其他人使用它:

Lambda Expression using Foreach Clause

Possible Duplicate: Why is there not a ForEach extension method on the IEnumerable interface? EDIT For reference, here's the blog post which eric referrrred to in the comments http://blogs.msdn.com/ericlippert/archive/2009/05/18/foreach-vs-foreach.aspx ORIG More of a curiosity I suppose but one for the C# Specification Savants... Why is it that the ForEach() clause doesn't w

使用Foreach子句的Lambda表达式

可能重复: 为什么IEnumerable界面上没有ForEach扩展方法? 编辑 作为参考,这里是eric在评论中提到的博客文章 http://blogs.msdn.com/ericlippert/archive/2009/05/18/foreach-vs-foreach.aspx 弊 我猜想有更多的好奇心,但对C#规范Savants的一个好奇... 为什么ForEach()子句不能在IQueryable / IEnumerable结果集上使用(或不可用)... 你必须首先转换你的结果ToList()或ToArray()推测这是对C#迭代IEn

What are the uses of "using" in C#

User kokos answered the wonderful Hidden Features of C# question by mentioning the using keyword. Can you elaborate on that? What are the uses of using ? The reason for the using statement is to ensure that the object is disposed as soon as it goes out of scope, and it doesn't require explicit code to ensure that this happens. As in Understanding the 'using' statement in C#, the

在C#中使用“using”有什么用途

用户kokos通过提及using关键字来回答C#问题的精彩隐藏特性。 你能详细说明一下吗? 什么是用途using ? using语句的原因是为了确保对象在超出作用域时尽快处理,并且不需要显式代码来确保发生这种情况。 就像理解C#中的'using'语句一样,.NET CLR也会转换 using (MyResource myRes = new MyResource()) { myRes.DoSomething(); } 至 { // Limits scope of myRes MyResource myRes= new MyResource();

What's the strangest corner case you've seen in C# or .NET?

I collect a few corner cases and brain teasers and would always like to hear more. The page only really covers C# language bits and bobs, but I also find core .NET things interesting too. For example, here's one which isn't on the page, but which I find incredible: string x = new string(new char[0]); string y = new string(new char[0]); Console.WriteLine(object.ReferenceEquals(x, y));

C#或.NET中见过的最奇怪的角落案例是什么?

我收集了几个角落案例和脑筋急转弯,并且总是希望听到更多。 该页面只包含C#语言位和bobs,但我也发现核心.NET的东西也很有趣。 例如,这里有一个不在页面上,但我觉得令人难以置信: string x = new string(new char[0]); string y = new string(new char[0]); Console.WriteLine(object.ReferenceEquals(x, y)); 我希望打印False - 毕竟,“新”(带有引用类型)总是会创建一个新对象,不是吗? C#和CLI的规范都表明它应

An Unknown feature in C# Maybe Just For Me

Possible Duplicate: Hidden Features of C#? What is it? Is it useful? Where? ?? It work this way; Object a = otherObject ?? "otherObject is null"; It means otherObject will be assigned to "a" if it is not null. If it is null the object at right will be assigned to "a". Its useful for me when I wanna set default values; public class MyClass { private Object b;

C#中的一个未知功能可能只是为了我

可能重复: C#的隐藏特性? 它是什么? 它有用吗? 哪里? ?? 它以这种方式工作; Object a = otherObject ?? "otherObject is null"; 这意味着如果otherObject不为null,它将被分配给“a”。 如果它为空,则将右侧的对象分配给“a”。 它对我有用,当我想设置默认值; public class MyClass { private Object b; public MyClass(Object a) { b = a ?? "Deafult Value"; } } 官方文件也可以在