RuntimeDefaults Class
The RuntimeDefaults class is a small helper in the PocoDataSet.Data assembly
that provides a runtime equivalent of default(T) when you only have a System.Type
instance.
It returns null for reference types and creates a default instance for value types
(for example, 0 for int, false for bool,
DateTime.MinValue for DateTime).
Namespace: PocoDataSet.Data
Assembly: PocoDataSet.Data.dll
public static class RuntimeDefaults
{
public static object? GetDefault(Type t);
public static object? ForType(Type t);
}
Returns the runtime default value for the provided Type.
This method is a small wrapper around ForType(Type).
t is null, throws ArgumentNullException.t is a value type, attempts to create the default value using Activator.CreateInstance.t is a reference type, returns null.
Same behavior as GetDefault(Type). It is kept for callers that already use the
ForType naming.
t is null, throws ArgumentNullException.t is a value type, it tries to create a default instance using
Activator.CreateInstance(t).
If instance creation fails for any reason, it returns null.
t is a reference type, returns null.
// Example: obtain default values when only a System.Type is known at runtime
object? defaultInt = RuntimeDefaults.GetDefault(typeof(int));
object? defaultBool = RuntimeDefaults.GetDefault(typeof(bool));
object? defaultString = RuntimeDefaults.GetDefault(typeof(string));
// defaultInt is 0 (boxed)
// defaultBool is false (boxed)
// defaultString is null
Activator.CreateInstance makes this helper work for any value type
(including structs) without hard-coding type checks.
try/catch and returns null
if instantiation fails. This keeps callers simple in scenarios where the type is unexpected or cannot be constructed.
DataTypeNames),
use MetadataDefaults / MetadataDefaultsProvider. RuntimeDefaults
is strictly about System.Type.
Table of Content POCO DataSet PocoDataSet.Data
Business Process Programming in .Net
© 2004–2026 Laskarzhevsky Software Inc.
Unless otherwise noted, the content of this website is licensed under the
Creative Commons Attribution 4.0 International License (CC BY 4.0).
Code examples are provided under the MIT License.
You are free to share and adapt the material provided that appropriate
credit is given and any modifications are clearly indicated.
The information provided on this website is for educational purposes only.
The author and publisher make no warranties regarding the completeness
or suitability of the information and are not responsible for any damages
resulting from its use.