title: Best way to test if a generic type is a string? (C#)
body: <p>I have a generic class that should allow any type, primitive or otherwise. The only problem with this is using <code>default(T)</code>. When you call default on a value type or a string, it initializes it to a reasonable value (such as empty string). When you call <code>default(T)</code> on an object, it returns null. For various reasons we need to ensure that if it is not a primitive type, then we will have a default instance of the type, <strong>not</strong> null. Here is attempt 1:</p>
  
  <pre><code>T createDefault()
  {
      if(typeof(T).IsValueType)
      {
          return default(T);
      }
      else
      {
          return Activator.CreateInstance&lt;T&gt;();
      }
  }
  </code></pre>
  
  <p>Problem - string is not a value type, but it does not have a parameterless constructor. So, the current solution is:</p>
  
  <pre><code>T createDefault()
  {
      if(typeof(T).IsValueType || typeof(T).FullName == "System.String")
      {
          return default(T);
      }
      else
      {
          return Activator.CreateInstance&lt;T&gt;();
      }
  }
  </code></pre>
  
  <p>But this feels like a kludge. Is there a nicer way to handle the string case?</p>