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
LVL 1
ValimaiAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
ValimaiAuthor Commented:
thanks, just wanted to confirm. thanks for the little extra tip too.
0
JimBrandleyCommented:
My pleasure. Good luck.

Jim
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.