Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6363265
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T00:01:00+00:00 2026-05-25T00:01:00+00:00

i written code using for loop in 2 ways like below first way for(int

  • 0

i written code using for loop in 2 ways like below

first way

for(int i=0;i<myList.Items.Count;i++)
{
.....
.....
.....
}

second way

int itemsCount = myList.Items.Count;
for(int i=0;i<itemsCount;i++)
{
.....
.....
.....
}

in my view
first way is better even though each and every incremnt of i value its checking
(i< myList.Items.Count)
because at runtime it simply replace myList.Items.Count with memeroy refrence
so according to mine if i taken a new local variable to assign count of items its just a wastage of memory.

i want to know how it acts when i written code like above and tel me which way is more better in the view of performance aspect

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T00:01:00+00:00Added an answer on May 25, 2026 at 12:01 am

    I believe the first way is (in some compilers) the more optimal way in some circumstances.

    You do not state a language, but it could be c# – so I will answer for that compiler. In C# I believe the former way of running is faster as it allows the compiler to ditch array bounds checking if you are accessing members of items in your loop. If you use an itemsCount variable it becomes more complicated for the compiler to track that the variable used in the loop has a safe value. This is only the case for a locally scoped array.

    That argument may hold for other languages like Java that might use a similar looking loop.

    Here is another question that addresses this question with for-loops in C#.

    I would also add (following the comment from @Evan on the question) that optimization like this is seldom necessary, unless you have a specific need for a fast section of code. Write what is the most readable and less likely to cause issues first, then optimize where necessary.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In the below written code, i want the second while loop to read from
I have just been re-working an old bit of compiler-like code written using bison.
i've written a for loop in Objective-C, This is how my code looks like
I've written some code using the REST starter kit and it works fine on
I have written some code to look at properties using reflection. I have retrieved
I have this code written in .NET 4.0 using VS2010 Ultimate Beta 2: smtpClient.Send(mailMsg);
I have some old code written in C for 16-bit using Borland C++ that
I'm using Windows CE Platform Builder and my code is written in C++ .
I'm just starting out using Ruby and I've written a bit of code to
I have written a cascading drop down list using JQuery. The code that I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.