{"id":2709,"date":"2019-10-24T13:34:24","date_gmt":"2019-10-24T12:34:24","guid":{"rendered":"https:\/\/solidt.eu\/site\/?p=2709"},"modified":"2024-11-13T11:09:11","modified_gmt":"2024-11-13T10:09:11","slug":"c-isnumeric","status":"publish","type":"post","link":"https:\/\/solidt.eu\/site\/c-isnumeric\/","title":{"rendered":"C# IsNumeric"},"content":{"rendered":"\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"csharp\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">\/\/ Preferred way: works with nullable types! (but a null value returns false)\npublic static bool IsNumeric(object o) => o is byte || o is sbyte || o is ushort || o is uint || o is ulong || o is short || o is int || o is long || o is float || o is double || o is decimal;<\/pre>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"csharp\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">\/\/ Another way based on a type instead (not preferred in most cases)\npublic static bool IsNumeric(Type type)\n{\n\tswitch (Type.GetTypeCode(type))\n\t{\n\t\tcase TypeCode.Byte:\n\t\tcase TypeCode.SByte:\n\t\tcase TypeCode.UInt16:\n\t\tcase TypeCode.UInt32:\n\t\tcase TypeCode.UInt64:\n\t\tcase TypeCode.Int16:\n\t\tcase TypeCode.Int32:\n\t\tcase TypeCode.Int64:\n\t\tcase TypeCode.Decimal:\n\t\tcase TypeCode.Double:\n\t\tcase TypeCode.Single:\n\t\t\treturn true;\n\t\tcase TypeCode.Object:\n\t\t\tif (type.IsGenericType &amp;&amp; type.GetGenericTypeDefinition() == typeof(Nullable&lt;>))\n\t\t\t{\n\t\t\t\treturn IsNumeric(Nullable.GetUnderlyingType(type));\n\t\t\t}\n\t\t\treturn false;\n\t\tdefault:\n\t\t\treturn false;\n\t}\n}<\/pre>\n\n\n\n<div style=\"height: 250px; position:relative; margin-bottom: 50px;\" class=\"wp-block-simple-code-block-ace\"><pre class=\"wp-block-simple-code-block-ace\" style=\"position:absolute;top:0;right:0;bottom:0;left:0\" data-mode=\"csharp\" data-theme=\"monokai\" data-fontsize=\"14\" data-lines=\"Infinity\" data-showlines=\"true\" data-copy=\"false\">bool IsStringOrNumericType(Type type)\n{\n    \/\/ Haal het onderliggende type op als het een nullable type is.\n    Type underlyingType = Nullable.GetUnderlyingType(type) ?? type;\n\n    \/\/ Controleer of het onderliggende type een string of numeriek type is.\n    return underlyingType == typeof(string) ||\n           underlyingType == typeof(byte) || underlyingType == typeof(sbyte) ||\n           underlyingType == typeof(short) || underlyingType == typeof(ushort) ||\n           underlyingType == typeof(int) || underlyingType == typeof(uint) ||\n           underlyingType == typeof(long) || underlyingType == typeof(ulong) ||\n           underlyingType == typeof(float) || underlyingType == typeof(double) ||\n           underlyingType == typeof(decimal);\n}<\/pre><\/div>\n","protected":false},"excerpt":{"rendered":"","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"inline_featured_image":false,"footnotes":""},"categories":[6,4,1],"tags":[],"class_list":["post-2709","post","type-post","status-publish","format-standard","hentry","category-dotnet","category-programming","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/posts\/2709","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/comments?post=2709"}],"version-history":[{"count":4,"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/posts\/2709\/revisions"}],"predecessor-version":[{"id":9030,"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/posts\/2709\/revisions\/9030"}],"wp:attachment":[{"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/media?parent=2709"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/categories?post=2709"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/tags?post=2709"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}