title: C# 4.0 - How to Handle Optional String Parameters
body: <p>This code is not valid:</p>
  
  <pre><code>private void Foo(string optionalString = string.Empty)
  {
     // do foo.
  }
  </code></pre>
  
  <p>But this code is:</p>
  
  <pre><code>private void Foo(string optionalString = "")
  {
     // do foo.
  }
  </code></pre>
  
  <p>Why? Because string.Empty is a readonly field, not a constant, and defaults for optional parameters must be a compile-time constant.</p>
  
  <p>So, onto my question... (well, concern)</p>
  
  <p>This is what i've had to do:</p>
  
  <pre><code>private const string emptyString = "";
  
  private void Foo(string optionalString = emptyString)
  {
     // do foo.
     if (!string.IsNullOrEmpty(optionalString))
        // etc
  }
  </code></pre>
  
  <p>How do you guys handle optional string parameters?</p>
  
  <p>Why can they not make <a href="http://msdn.microsoft.com/en-us/library/system.string.empty.aspx" rel="noreferrer">String.Empty</a> a compile-time constant?</p>