Default value of a type at Runtime - Printable Version +- 0Day Forums (https://zeroday.vip) +-- Forum: Coding (https://zeroday.vip/Forum-Coding) +--- Forum: C# (https://zeroday.vip/Forum-C) +--- Thread: Default value of a type at Runtime (/Thread-Default-value-of-a-type-at-Runtime) |
Default value of a type at Runtime - interpulmonary503175 - 07-24-2023 For any given type i want to know its default value. In C#, there is a keyword called default for doing this like object obj = default(Decimal); but I have an instance of Type (called myType) and if I say this, object obj = default(myType); it doesn't work Is there any good way of doing this? I know that a huge switch block will work but thats not a good choice. RE: Default value of a type at Runtime - greaseproofness134086 - 07-24-2023 How about something like... class Program { static void Main(string[] args) { PrintDefault(typeof(object)); PrintDefault(typeof(string)); PrintDefault(typeof(int)); PrintDefault(typeof(int?)); } private static void PrintDefault(Type type) { Console.WriteLine("default({0}) = {1}", type, DefaultGenerator.GetDefaultValue(type)); } } public class DefaultGenerator { public static object GetDefaultValue(Type parameter) { var defaultGeneratorType = typeof(DefaultGenerator<>).MakeGenericType(parameter); return defaultGeneratorType.InvokeMember( "GetDefault", BindingFlags.Static | BindingFlags.Public | BindingFlags.InvokeMethod, null, null, new object[0]); } } public class DefaultGenerator<T> { public static T GetDefault() { return default(T); } } It produces the following output: default(System.Object) = default(System.String) = default(System.Int32) = 0 default(System.Nullable`1[System.Int32]) = RE: Default value of a type at Runtime - gonad98 - 07-24-2023 What do you mean by "Default Value"? All reference Types ("class") have null as default value, while all value types will have their default values according to [this table][1]. [1]: [To see links please register here] RE: Default value of a type at Runtime - wilhelmina140 - 07-24-2023 Here's a function that will return the default value for a nullable type (in other words, it returns 0 for both `Decimal` and `Decimal?`): public static object DefaultValue(Type maybeNullable) { Type underlying = Nullable.GetUnderlyingType(maybeNullable); if (underlying != null) return Activator.CreateInstance(underlying); return Activator.CreateInstance(maybeNullable); } RE: Default value of a type at Runtime - piccolo283 - 07-24-2023 You could also add it as an extension method to System.Type: public static class TypeExtensions { public static object GetDefaultValue(this Type t) { if (t.IsValueType && Nullable.GetUnderlyingType(t) == null) return Activator.CreateInstance(t); else return null; } } RE: Default value of a type at Runtime - ectolophlobbb - 07-24-2023 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). |