title: Local variable inside recursive function unassigned
body: <p>I have the following code, any ideas on how to resolve this issue, instead of declaring a int variable outside the function? I get the following compiler error: Use of unassigned local variable 'counter'</p>
  
  <pre><code>public static int GetNumberOfDevicesForManagementGroup(Guid managementGroupId, bool firstTime)
    {
       int counter;
       using (var ctx = new DeviceManagerEntities())
       {
          if (firstTime)
          {
             firstTime = false;
             counter = 0;
             GetNumberOfDevicesForManagementGroup(managementGroupId, firstTime);
          }
          else
          {
             var groups = ctx.ManagementGroups
                .Where(x =&gt; x.ParentId == managementGroupId)
                .ToList();
             if (groups.Count != 0)
             {
                foreach (ManagementGroups group in groups)
                {
                   var devices = ctx.Devices
                      .Where(x =&gt; x.ManagementGroups.ManagementGroupId == group.ManagementGroupId)
                      .ToList();
                   foreach (Devices device in devices)
                   {
                      counter++;
                   }
                   GetNumberOfDevicesForManagementGroup(group.ManagementGroupId, firstTime);
                }
             }
             else
             {
                var devices = ctx.Devices
                      .Where(x =&gt; x.ManagementGroups.ManagementGroupId == managementGroupId)
                      .ToList();
                foreach (Devices device in devices)
                {
                   counter++;
                }
             }
          }
       }
       return counter;
    }
  </code></pre>