Default Value of a Type at Runtime

Default value of a type at Runtime

There's really only two possibilities: null for reference types and new myType() for value types (which corresponds to 0 for int, float, etc) So you really only need to account for two cases:

object GetDefaultValue(Type t)
{
if (t.IsValueType)
return Activator.CreateInstance(t);

return null;
}

(Because value types always have a default constructor, that call to Activator.CreateInstance will never fail).

Getting the default of an unknown type at runtime

== for object will always mean: reference equality. For a reference, the default is always null, so if !prop.PropertyType.IsValueType, then you only need a null check. For value-types, you will be boxing. So reference equality will always report false, unless they are both Nullable<T> for some T, and both are empty. However, to get a "default" value-type (prop.PropertyType.IsValueType), you can use Activator.CreateInstance(prop.PropertyType). Just keep in mind that == is not going to do what you want here. Equals(x,y) might work better.

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

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.

In .NET, at runtime: How to get the default value of a type from a Type object?

I think that Frederik's function should in fact look like this:

public object GetDefaultValue(Type t)
{
if (t.IsValueType)
{
return Activator.CreateInstance(t);
}
else
{
return null;
}
}

Programmatic equivalent of default(Type)

  • In case of a value type use Activator.CreateInstance and it should work fine.
  • When using reference type just return null
public static object GetDefault(Type type)
{
if(type.IsValueType)
{
return Activator.CreateInstance(type);
}
return null;
}

In the newer version of .net such as .net standard, type.IsValueType needs to be written as type.GetTypeInfo().IsValueType

Can a C++ class's default value be changed at runtime?

Just because you can, doesn't mean you should. Now that I got that out of the way, here's how:

#include <iostream>

struct C {
static int default_val;
C(int i = default_val) {
std::cout << i << '\n';
}
};

int C::default_val = 0;

int main() {
C c;

C::default_val = 1;

C c2;

return 0;
}

A default argument to a function doesn't have to be a constant expression, it just has to be "available" at every place the function is called. And a static variable is just that.

It also doesn't have to be accessible at the call site. So if you want the class to control it, but not have it be modifiable from outside the class, you can make it private.

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

From this post:

public object GetDefaultValue(Type t)
{
if (t.IsValueType) {
return Activator.CreateInstance(t);
} else {
return null;
}

How to find default property values of a control at runtime in C#

This is a good opportunity to use reflection! Here's a method which should get the default value of any property for any type which has a default constructor (public, no parameters):

public static object GetDefaultPropertyValue(Type type, string propertyName)
{
if (type.GetConstructor(new Type[] { }) == null)
throw new Exception(type + " doesn't have a default constructor, so there is no default instance to get a default property value from.");
var obj = Activator.CreateInstance(type);
return type.GetProperty(propertyName).GetValue(obj, new object[] { });
}

Note that if you are doing this with a large number of controls of which there can be multiples of a single type, you may want to cache the results for each type, as reflection is somewhat slow.

Non-generic function to get the default value of a system.type

I think that there is a very simple approach to solving this problem: if the type is not a value type, return nothing, otherwise return a new instance of the type, which will be initialized to the default value:

Public Shared Function DefaultValue(Type As System.Type) As Object
If Not Type.IsValueType Then
Return Nothing
Else
Return Activator.CreateInstance(Type)
End If
End Function


Related Topics



Leave a reply



Submit