110

I have a generic class that should allow any type, primitive or otherwise. The only problem with this is using default(T). 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 default(T) 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, not null. Here is attempt 1:

T createDefault()
{
    if(typeof(T).IsValueType)
    {
        return default(T);
    }
    else
    {
        return Activator.CreateInstance<T>();
    }
}

Problem - string is not a value type, but it does not have a parameterless constructor. So, the current solution is:

T createDefault()
{
    if(typeof(T).IsValueType || typeof(T).FullName == "System.String")
    {
        return default(T);
    }
    else
    {
        return Activator.CreateInstance<T>();
    }
}

But this feels like a kludge. Is there a nicer way to handle the string case?

wonea
  • 4,783
  • 17
  • 86
  • 139
Rex M
  • 142,167
  • 33
  • 283
  • 313

5 Answers5

196

Keep in mind that default(string) is null, not string.Empty. You may want a special case in your code:

if (typeof(T) == typeof(String)) return (T)(object)String.Empty;
Matt Hamilton
  • 200,371
  • 61
  • 386
  • 320
  • 2
    I thought I tried that solution earlier and it did not work, but I must've done something stupid. And thanks for pointing out default(string) returns null, we did not run into an error yet because of it, but that is true. – Rex M May 29 '10 at 00:32
  • 1
    @Matt Hamilton: +1, but you should update your answer to return '(T)(object)String.Empty' as suggested by CodeInChaos because the method return type is generic, you can not just return string. – VoodooChild Jan 23 '12 at 17:04
  • 2
    What about the `is` keyword? Isn't that of use here ? – Naveed Butt Sep 15 '14 at 07:21
  • At the moment is not possible apply the is operator with generics and assignment  or direct instancing, isn't it?, will be a cool feature – Juan Pablo Garcia Coello Jan 18 '17 at 07:05
14
if (typeof(T).IsValueType || typeof(T) == typeof(String))
{
     return default(T);
}
else
{
     return Activator.CreateInstance<T>();
}

Untested, but the first thing that came to mind.

Rex M
  • 142,167
  • 33
  • 283
  • 313
FlySwat
  • 172,459
  • 74
  • 246
  • 311
4

You can use the TypeCode 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 "Generic Type Checking".

Community
  • 1
  • 1
jfs
  • 16,758
  • 13
  • 62
  • 88
3

Personally, I like method overloading:

public static class Extensions { 
  public static String Blank(this String me) {      
    return String.Empty;
  }
  public static T Blank<T>(this T me) {      
    var tot = typeof(T);
    return tot.IsValueType
      ? default(T)
      : (T)Activator.CreateInstance(tot)
      ;
  }
}
class Program {
  static void Main(string[] args) {
    Object o = null;
    String s = null;
    int i = 6;
    Console.WriteLine(o.Blank()); //"System.Object"
    Console.WriteLine(s.Blank()); //""
    Console.WriteLine(i.Blank()); //"0"
    Console.ReadKey();
  }
}
theoski
  • 109
  • 1
  • 5
-6

The discussion for String is not working here.

I had to have following code for generics to make it work -

   private T createDefault()
    { 

        {     
            if(typeof(T).IsValueType)     
            {         
                return default(T);     
            }
            else if (typeof(T).Name == "String")
            {
                return (T)Convert.ChangeType(String.Empty,typeof(T));
            }
            else
            {
                return Activator.CreateInstance<T>();
            } 
        } 

    }
Anil
  • 7
  • 4
    Testing for `String` by name, especially without regarding a namespace is bad. And I don't like the way you convert either. – CodesInChaos Feb 13 '11 at 12:30