Home     |     .Net Programming    |     cSharp Home    |     Sql Server Home    |     Javascript / Client Side Development     |     Ajax Programming

Ruby on Rails Development     |     Perl Programming     |     C Programming Language     |     C++ Programming     |     IT Jobs

Python Programming Language     |     Laptop Suggestions?    |     TCL Scripting     |     Fortran Programming     |     Scheme Programming Language


 
 
Cervo Technologies
The Right Source to Outsource

MS Dynamics CRM 3.0

C# Programming

Newbie Q: Declare variable IN the loop or BEFORE the loop?


Hi All,
I have a somewhat trivial newbie question - but I'm trying to "get" C#
more and more.  Here goes:  Let's say I have a loop:

for (int i = 0;i <= MyCollection.Count - 1;i++)
{
     int x = i + 2;
      // do whatever, some code using i and some code using x (which
is always 2 greater than i)
 }

Now, coming from a traditional VB 6 background, I would normally
declare x BEFORE the loop, and then use "x = i + 2:" within the loop.
Because it seems "wasteful" to keep declaring a NEW variable x WITH
EVERY ITERATION of the loop.

Is it indeed "wasteful" to declare x as the code shows above?
And - maybe more importantly - what is STANDARD PRACTICE in the C#
world in this case?

Thanks much,

Rex

On Jun 7, 12:08 pm, Rex <RexForum4@OneSimpleHabit.com> wrote:

> I have a somewhat trivial newbie question - but I'm trying to "get" C#
> more and more.  Here goes:  Let's say I have a loop:

> for (int i = 0;i <= MyCollection.Count - 1;i++)
> {
>      int x = i + 2;
>       // do whatever, some code using i and some code using x (which
> is always 2 greater than i)
>  }

> Now, coming from a traditional VB 6 background, I would normally
> declare x BEFORE the loop, and then use "x = i + 2:" within the loop.
> Because it seems "wasteful" to keep declaring a NEW variable x WITH
> EVERY ITERATION of the loop.

You say it *seems* wasteful - I'm not familiar enough with VB6 to know
whether it actually *is* wasteful in that concext.

> Is it indeed "wasteful" to declare x as the code shows above?

No.

> And - maybe more importantly - what is STANDARD PRACTICE in the C#
> world in this case?

Declare a variable with as little scope as possible, generally. It
makes it clear that you're not intending to use the variable outside
that scope.

Jon

-----------------------------------------------Reply-----------------------------------------------

The IL will be unaffected (IL variables are all declared before the
code) - so no, it isn't any more wasteful. There is only one real (IL)
variable that gets re-used (either way).

As such, I prefer to declare *inside* the loop under the "limit the
scope whenever possible" approach; note that the only time there is a
functional difference is when you assign the variable outside of the
loop, and / or (more importantly) use the existing value without
assigning it first - i.e. there is a huge difference between

int i = 0;
for {
  i++;

}

and
for {
  int i = 0;
  i++;

}

In one case i is always 1, in t'other it increments gradually.

Marc

-----------------------------------------------Reply-----------------------------------------------

On Jun 7, 12:18 pm, "Marc Gravell" <marc.grav@gmail.com> wrote:

> The IL will be unaffected (IL variables are all declared before the
> code) - so no, it isn't any more wasteful. There is only one real (IL)
> variable that gets re-used (either way).

> As such, I prefer to declare *inside* the loop under the "limit the
> scope whenever possible" approach; note that the only time there is a
> functional difference is when you assign the variable outside of the
> loop, and / or (more importantly) use the existing value without
> assigning it first - i.e. there is a huge difference between

There's one other difference, which relates to the variable's
*lifetime* as opposed to just its scope, with respect to captured
variables in anonymous methods. As such, just the other day I had a
loop like this:

foreach (string url in someListOfUrls)
{
    string copyOfUrl = url;
    ThreadStart ts = delegate() { DoSomethingWith(copyOfUrl); };
    new Thread(ts).Start();

}

At first glance, you'd expect to be able to remove copyOfUrl - but the
lifetime of url is the lifetime of the whole loop, and thus shared
between all the ThreadStart delegates, whereas the lifetime of
copyOfUrl is just that iteration of the loop. Declaring copyOfUrl
outside the loop would defeat its purpose.

Jon

-----------------------------------------------Reply-----------------------------------------------

deja vu ;-p

Marc

-----------------------------------------------Reply-----------------------------------------------

found it!
http://groups.google.co.uk/group/microsoft.public.dotnet.languages.cs...

Yes - I failed again ;-p

Marc

Add to del.icio.us | Digg this | Stumble it | Powered by Megasolutions Inc