In what order does a C# for each loop iterate over a List?

前端 未结 6 1189
南旧
南旧 2020-12-03 00:35

I was wondering about the order that a foreach loop in C# loops through a System.Collections.Generic.List object.

I found another question abou

6条回答
  •  抹茶落季
    2020-12-03 01:07

    I've just had to do something similar as a quick hack of code, though it didn't work for what I was trying to do it did reorder the list for me.

    Using LINQ to change the order

             DataGridViewColumn[] gridColumns = new DataGridViewColumn[dataGridView1.Columns.Count];
             dataGridView1.Columns.CopyTo(gridColumns, 0); //This created a list of columns
    
             gridColumns = (from n in gridColumns
                            orderby n.DisplayIndex descending
                            select n).ToArray(); //This then changed the order based on the displayindex
    

提交回复
热议问题