469,578 Members | 1,257 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,578 developers. It's quick & easy.

Simple question on declaring a variable

Dom
I should know this, but I don't. In the following, is an integer
being created with each iteration?

for (...)
{
int n = SomeFunction ();
}

Or should I do the following, even if I don't plan to use "n" outside
the loop?

int n;
for (...)
{
n = SomeFunction ();
}
Dec 18 '07 #1
4 993
Hi,
--
Ignacio Machin
http://www.laceupsolutions.com
Mobile & warehouse Solutions.
"Dom" <do********@gmail.comwrote in message
news:0e**********************************@d27g2000 prf.googlegroups.com...
>I should know this, but I don't. In the following, is an integer
being created with each iteration?
No, only 1 position in reserved in memory for the variable.

for (...)
{
int n = SomeFunction ();
}

Or should I do the following, even if I don't plan to use "n" outside
the loop?
You will have the same memory comsuption in both cases. but they are not the
same!
In this second case the variable can be referenced outside the for loop. In
the first case n only exist inside the loop.
int n;
for (...)
{
n = SomeFunction ();
}


Dec 18 '07 #2
In the general case it makes no difference and compiles to the same
thing. I would use the first syntax just to limit the scope.

Caveat: if the variable is "captured" inside an anonymous method (or
lambda in C# 3), then note that then lifetime and separation relates
to the code-block which declares it; but you don't seem to be doing
that here...

Marc
Dec 18 '07 #3
Dom wrote:
I should know this, but I don't. In the following, is an integer
being created with each iteration?

for (...)
{
int n = SomeFunction ();
}

Or should I do the following, even if I don't plan to use "n" outside
the loop?

int n;
for (...)
{
n = SomeFunction ();
}
It does not matter functionally, but I would recommend the first,
because it limits the scope of n.

Arne
Dec 22 '07 #4
Arne Vajhøj <ar**@vajhoej.dkwrote:

<snip>
It does not matter functionally, but I would recommend the first,
because it limits the scope of n.
Not disagreeing with you, but it's worth emphasising that as Marc said,
it *does* matter in more complex code where the variable is captured.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
World class .NET training in the UK: http://iterativetraining.co.uk
Dec 22 '07 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by Basil Fawlty | last post: by
2 posts views Thread by TNGgroup | last post: by
1 post views Thread by Maarten Terlingen | last post: by
6 posts views Thread by Mark A. Sam | last post: by
8 posts views Thread by rendle | last post: by
13 posts views Thread by aaragon | last post: by
3 posts views Thread by deejayquai | last post: by
30 posts views Thread by galiorenye | last post: by
reply views Thread by suresh191 | last post: by
4 posts views Thread by guiromero | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.