C#: How to find the default value for a run-time Type?

Posted by George Mauer on Stack Overflow See other posts from Stack Overflow or by George Mauer
Published on 2010-06-09T20:00:06Z Indexed on 2010/06/09 20:02 UTC
Read the original article Hit count: 142

Filed under:
|

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) {
  ....
}

© Stack Overflow or respective owner

Related posts about c#

Related posts about strong-typing