I need a function that does this:
private static dynamic Zero(Type T) { if (T == typeof(Decimal)) { return Decimal.Zero; } else if (T == typeof(Double)) { return new Double(); } else if (T == typeof(Int64)) { return new Int64(); } ... }
But for all types. I would like you not to write an expression about the giant. Is there any other way to do this? I am using C # 4.0.
The default constructor will be used for the value type.
if(T.IsValueType()) return Activator.CreateInstance(T);
Then you can do other things, for example, testing the Zero method by type and, if so, calling it.
return default(T);
No need dynamichere:
dynamic
private static T Zero<T>() { return default(T); }
Source: https://habr.com/ru/post/1729005/More articles:Database Question - Thrashing - What is? - terminologyAre there any optimizations to get the return value in Delphi? - optimizationPostSharp does not affect speed - performancehttps://translate.googleusercontent.com/translate_c?depth=1&pto=aue&rurl=translate.google.com&sl=ru&sp=nmt4&tl=en&u=https://fooobar.com/questions/1729003/given-a-pointer-how-might-i-find-the-heapentry-that-it-belongs-to&usg=ALkJrhhn3x_RUHmptnT9X1_F5k7gdVJYwQEclipse does not start after uninstalling Java and reinstalling to a new location - javaWhen a PHP function is called, and something is RETURNED, does it stop running the code under it? - phpAssociate Wpf HierarchicalDataTemplate ItemsSource with CollectionViewSource in a Dictionary? - data-bindingEvaluating environment variables in a command executed by Java Runtime.exec () - javaHow to make a div from foaming - htmlGit-svn operation philosophy? - gitAll Articles