Q:

Can I clear an external variable inside a using block?

I have a static StreamWriter field for a log file I need to access through a lambda function that listens to StandardOutput on a long-running Process.
I'm using the null/not-null status of the field to determine if the Process is busy on another thread; the actions need to be performed sequentially.
My question is, what happens when I set my variable to null inside a using block? Will it still get disposed properly?
public class Service
{
    private static StreamWriter logger;

    void Run(string logFile)
    {
        using (logger = new StreamWriter(logFile))
        {
            /* ... */

            logger = null;
        }
    }
}

A:

According to the C# reference, § 8.13, your code:
private static StreamWriter logger;

using (logger = new StreamWriter(logFile))
{
   /* ... */
   logger = null;
}

is equivalent to
private static StreamWriter logger;

{  // using scope
   logger = new StreamWriter(logFile);
   IDisposable resource = logger;       // hidden var inserted by the compiler 
   try
   {
     /* ... */
     logger = null;
   }
   finally
   {
      if (resource != null)  
        resource.Dispose();
   }
}

The relevant quote:

A using statement of the form

    using (expression) statement

has the same three possible expansions, but in this case ResourceType is implicitly the compile-time type of the expression, and the resource variable is inaccessible in, and invisible to, the embedded statement.

