So given a static type in your code you can do
var defaultMyTypeVal = default(MyType);
How would you do the same thing given a variable of Type so you can use it during runtime?
In other words how do I implement the following method without a bunch of if statements or using Generics (because I will not know the type I'm passing into the method at compile time)?
public object GetDefaultValueForType(Type type) {
....
}
From this post:
public object GetDefaultValue(Type t)
{
if (t.IsValueType) {
return Activator.CreateInstance(t);
} else {
return null;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With