Thursday, August 19, 2010

C#: How to get the default value for a type using reflection

The following code snippet gives you the default value for the given type:

public static object GetDefaultValue(Type type)
{
 object value = null;

 if (type.IsValueType)
  value = Activator.CreateInstance(type);
 else
  value = null;

 return value;
}