tags:

views:

4875

answers:

4

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?

+4  A: 
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.

FlySwat
+10  A: 

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 String.Empty;
Matt Hamilton
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
A: 

@jonathan T already represents a type, not an object instance, so we cannot call GetType() or any other method on it.

Right, which is why I edited my post immediately after posting it.

FlySwat
+1  A: 

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

John