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 5941797
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T16:09:38+00:00 2026-05-22T16:09:38+00:00

ReSharper usually suggests me that, and I’m still looking for a good reason of

  • 0

ReSharper usually suggests me that, and I’m still looking for a good reason of why to do that.

The only thing that came to my mind is that declaring it closer to the scope it will be used, can avoid initializing it in some cases where it isn’t necessary (because a condition, etc.)

Something related with that is the following:

int temp;
foreach (var x in collection) { 
    temp = x.GetValue();
    //Do something with temp
}

Is that really different than

foreach (var x in collection) {
    int temp = x.GetValue();
    //...
}

I mean, isn’t the second code more expensive because it is allocating memory everytime? Or are both the same? Of course, after finished the loop, in the second code the garbage collector will take care about temp variable, but not in the first one…

  • 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-22T16:09:39+00:00Added an answer on May 22, 2026 at 4:09 pm

    The cost of the second example is negligible. The only difference is that in the first example, temp will be available outside the scope of the for loop, and thus it will exist longer than if you declared it inside the for loop.

    If you don’t need temp outside the for loop, it shouldn’t be declared outside that loop. Like others have said, readability and style are more at play here than performance and memory.

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

Sidebar

Related Questions

I wrote a Resharper plugin that seems to only work when users have the
I've upgraded Resharper from 5 version to 6 version. After that when I type
I've turned off ReSharper intellisense but still want a keyboard shortcut for opening the
Do you know of a ReSharper 5.1 plugin that allows me to go to
ReSharper 4.5's test runner will run MSTest tests out of the box, and that's
ReSharper suggests we change: System.Net.ServicePointManager.ServerCertificateValidationCallback += delegate(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors)
ReSharper sometimes hints that I can make some of my random utility methods in
ReSharper suggests changing the accessibility of a public constructor in an abstract class to
Resharper is killing me for some reason on startup, I just need to get
With ReSharper, I can go to next error in solution, but it seems that

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.