1

i have the following code below and was lookign to clean it up, but am not sure if it needs clean up or not.

foreach (SearchResult sr in mySrchColl)
{
    string strValIn = sr.Properties["in"][0].ToString();
    string strValOut= sr.Properties["out"][0].ToString();
}

do i change this to something like this:

string strValIn = "";
string strValOut= "";

foreach (SearchResult sr in mySrchColl)
            {
                strValIn = sr.Properties["in"][0].ToString();
                strValOut= sr.Properties["out"][0].ToString();
}

whats the difference or are these two identical?

kacalapy
  • 9,806
  • 20
  • 74
  • 119
  • I think you must be missing some code in your example. What are you going to do with those strings? – Don Kirkby Dec 10 '10 at 19:17
  • And what is the point of that code anyway? You may as well just assign the last element. – Ed S. Dec 10 '10 at 19:18
  • i use the string variables later in a call to a function that takes 10 params, so instead of having them all inline i could read what they are using meaningful variable names... – kacalapy Dec 10 '10 at 19:22

6 Answers6

4

In the first code segment, the variables will be accessible only within the loop. In the second code segment, they can be accessed outside the loop. This is known as Programming Scope

npinti
  • 51,780
  • 5
  • 72
  • 96
  • what about the system reclaiming resources for the variables? if the loop goes on for 1000 times will it matter? i think the resources reclamation will occur after each loop -am i correct? – kacalapy Dec 10 '10 at 19:25
  • According to http://social.msdn.microsoft.com/forums/en-US/csharplanguage/thread/d43aaba5-a58b-4610-bea4-5bc5d6741f98 and http://stackoverflow.com/questions/407255/difference-between-declaring-variables-before-or-in-loop, it wouldn't matter. – npinti Dec 10 '10 at 19:33
3

They compile down to the same IL...no performance difference. It would only be readability; if only used in the foreach...keep the declaration in there.

Aaron McIver
  • 24,527
  • 5
  • 59
  • 88
1

Difference between declaring variables before or in loop?

Community
  • 1
  • 1
Patrick
  • 7,512
  • 7
  • 39
  • 50
1

If you don't capture the variables into a lambda / anon method it won't make any difference. Inside is arguably cleaner.

As a side note; even if you declare them outside; you don't need to initialize it there; the following is valid:

string s;
foreach(...) {
    s = ...
}

I would still declare inside though :)

Marc Gravell
  • 1,026,079
  • 266
  • 2,566
  • 2,900
0

In dotnet strings are immutable, so both of the example codes are going to create new objects and assign them to the 2 variables. As such if you are not using those 2 variables outside the foreach loop there isnt going to be much difference.

0

I think the second one is better. Because in first one, you are creating reference and object of string in loop...whereas in second case , you are just creating ' object of string' and references are just changing...

Eternal Noob
  • 2,717
  • 5
  • 27
  • 41