[Webinar] Streamline your web hosting managementRegister Today

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 211
  • Last Modified:

List<MyClass> ;; MyClass.MyIndex, fastest way to re-index with seed int

hi

I have a class which has a property of MyIndex.
MyClass has a constructor
public MyClass(int myIndex) { this.MyIndex = myIndex; }

I fill a list with, lets assume, random index values.
List<MyClass> myList = new List<MyClass>();
myList.Add(new MyClass(99));
myList.Add(new MyClass(88));
myList.Add(new MyClass(33));
myList.Add(new MyClass(55));
myList.Add(new MyClass(11));

Now I want to reindex them in the order they were added. Is there a more efficient way to do the following? Assumption is 1000 records max.

int i = 0;
foreach (MyClass mc in myList)
{
    mc.Index = i;
    i++;
}

thanks
0
Valimai
Asked:
Valimai
  • 2
1 Solution
 
JimBrandleyCommented:
That is about as fast as it can be done. You can combine the two lines in the loop as:
int i = 0;
foreach (MyClass mc in myList)
{
    mc.Index = i++;
}

But that will not buy much improvement.

Jim
0
 
ValimaiAuthor Commented:
thanks, just wanted to confirm. thanks for the little extra tip too.
0
 
JimBrandleyCommented:
My pleasure. Good luck.

Jim
0

Featured Post

[Webinar] Improve your customer journey

A positive customer journey is important in attracting and retaining business. To improve this experience, you can use Google Maps APIs to increase checkout conversions, boost user engagement, and optimize order fulfillment. Learn how in this webinar presented by Dito.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now