Simple question on declaring a variable

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Dom

    #1

    Simple question on declaring a variable

    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 ();
    }


  • Ignacio Machin \( .NET/ C# MVP \)

    #2
    Re: Simple question on declaring a variable

    Hi,


    --
    Ignacio Machin
    The #1 Warehouse Management System & Direct Store Delivery Software (DSD) for QuickBooks & ERP Systems – LaceUp Solutions

    Mobile & warehouse Solutions.
    "Dom" <dolivastro@gma il.comwrote in message
    news:0ea6764f-1618-469d-96fd-076b5e97800b@d2 7g2000prf.googl egroups.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 ();
    }
    >
    >

    Comment

    • Marc Gravell

      #3
      Re: Simple question on declaring a variable

      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


      Comment

      • =?ISO-8859-1?Q?Arne_Vajh=F8j?=

        #4
        Re: Simple question on declaring a variable

        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

        Comment

        • Jon Skeet [C# MVP]

          #5
          Re: Simple question on declaring a variable

          Arne Vajhøj <arne@vajhoej.d kwrote:

          <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 - <skeet@pobox.co m>
          http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
          World class .NET training in the UK: http://iterativetraining.co.uk

          Comment

          Working...