How to know default value of Type
In generic approach I can use default keyword to know what is default value
public T GetDefaultValue<T>()
{
return default(T);
}
But if only has a System.Type, I can't use default keyword to do it....
public object GetDefaultValue(System.Type dataType)
{
return default(dataType); //Not work....
}
Please help
There's no way to do this in C# 2.0.
The workaround is cumbersome - from the type, you can instantiate an object (using TypeDescriptor.CreateInstance) with zero arguments. That will create a new object with default value. Now, you can return that object.