<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>

[{'answer_id': 31512, 'author': 'FlySwat', 'author_id': 1965, 'author_profile': 'https://Stackoverflow.com/users/1965', 'pm_score': 4, 'selected': False, 'text': '<pre><code>if (typeof(T).IsValueType || typeof(T) == typeof(String))\n{\n     return default(T);\n}\nelse\n{\n     return Activator.CreateInstance&lt;T&gt;();\n}\n</code></pre>\n\n<p>Untested, but the first thing that came to mind.</p>\n'}, {'answer_id': 31513, 'author': 'Matt Hamilton', 'author_id': 615, 'author_profile': 'https://Stackoverflow.com/users/615', 'pm_score': 9, 'selected': True, 'text': '<p>Keep in mind that default(string) is null, not string.Empty. You may want a special case in your code:</p>\n\n<pre><code>if (typeof(T) == typeof(String)) return (T)(object)String.Empty;\n</code></pre>\n'}, {'answer_id': 31529, 'author': 'jfs', 'author_id': 718, 'author_profile': 'https://Stackoverflow.com/users/718', 'pm_score': 2, 'selected': False, 'text': '<p>You can use the <a href="http://msdn.microsoft.com/en-us/library/system.typecode.aspx" rel="nofollow noreferrer">TypeCode</a> enumeration. Call the GetTypeCode method on classes that implement the IConvertible interface to obtain the type code for an instance of that class. IConvertible is implemented by Boolean, SByte, Byte, Int16, UInt16, Int32, UInt32, Int64, UInt64, Single, Double, Decimal, DateTime, Char, and String, so you can check for primitive types using this. More info on "<a href="https://stackoverflow.com/questions/8941/generic-type-checking#8956">Generic Type Checking</a>".</p>\n'}, {'answer_id': 4984105, 'author': 'Anil', 'author_id': 615020, 'author_profile': 'https://Stackoverflow.com/users/615020', 'pm_score': -1, 'selected': False, 'text': '<p>The discussion for String is not working here.</p>\n\n<p>I had to have following code for generics to make it work -</p>\n\n<pre><code>   private T createDefault()\n    { \n\n        {     \n            if(typeof(T).IsValueType)     \n            {         \n                return default(T);     \n            }\n            else if (typeof(T).Name == "String")\n            {\n                return (T)Convert.ChangeType(String.Empty,typeof(T));\n            }\n            else\n            {\n                return Activator.CreateInstance&lt;T&gt;();\n            } \n        } \n\n    }\n</code></pre>\n'}, {'answer_id': 28435923, 'author': 'theoski', 'author_id': 598807, 'author_profile': 'https://Stackoverflow.com/users/598807', 'pm_score': 2, 'selected': False, 'text': '<p>Personally, I like method overloading:  </p>\n\n<pre><code>public static class Extensions { \n  public static String Blank(this String me) {      \n    return String.Empty;\n  }\n  public static T Blank&lt;T&gt;(this T me) {      \n    var tot = typeof(T);\n    return tot.IsValueType\n      ? default(T)\n      : (T)Activator.CreateInstance(tot)\n      ;\n  }\n}\nclass Program {\n  static void Main(string[] args) {\n    Object o = null;\n    String s = null;\n    int i = 6;\n    Console.WriteLine(o.Blank()); //"System.Object"\n    Console.WriteLine(s.Blank()); //""\n    Console.WriteLine(i.Blank()); //"0"\n    Console.ReadKey();\n  }\n}\n</code></pre>\n'}]
['https://Stackoverflow.com/questions/31498', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/67/']