how to get the default value of a type if the type is only known as System.Type? [duplicate]

Since you really only have to worry about value types (reference types will just be null), you can use Activator.CreateInstance to call the default constructor on them.

public static object GetDefaultValue(Type type) {
   return type.IsValueType ? Activator.CreateInstance(type) : null;
}

Edit: Jon is (of course) correct. IsClass isn’t exhaustive enough – it returns False if type is an interface.

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)