Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Seems everyone is doing it really complicated, with long lists of conditions or big <code>switch</code> statements.</p> <p>There are multiple possible interpretations of what you think of as primitive types.</p> <h3>1. .NET primitive types</h3> <p>.NET has a list of types that it considers to be primitive types. On the <a href="http://msdn.microsoft.com/en-us/library/42892f65.aspx" rel="nofollow"><code>Type</code> class</a> there is a property <a href="http://msdn.microsoft.com/en-us/library/system.type.isprimitive.aspx" rel="nofollow"><code>IsPrimitive</code> property</a> that will return <code>true</code> for any of these primitive types and <code>false</code> for any other type.</p> <blockquote> <p>The primitive types are Boolean, Byte, SByte, Int16, UInt16, Int32, UInt32, Int64, UInt64, IntPtr, UIntPtr, Char, Double, and Single.</p> </blockquote> <p>Note that <code>IntPtr</code> and <code>UIntPtr</code> are also in there. They represent the platform-specific integer type (e.g. 32-bit integer on a 32-bit computer, 64-bit on a 64-bit computer). Also note that .NET does not consider <code>String</code> or <code>Decimal</code> to be a primitive.</p> <p>You can test it like this:</p> <pre><code>public static bool IsPrimitiveType(Type type) { return type.IsPrimitive; } </code></pre> <h3>2. .NET primitive types and String and Decimal</h3> <p>In your question you have included the <code>String</code> and <code>Decimal</code> types in your definition of a primitive type. Let's test for those too, like this:</p> <pre><code>public static bool IsPrimitiveType(Type type) { return type.IsPrimitive || type == typeof(decimal) || type == typeof(string); } </code></pre> <p>Since it is impossible to extend <code>String</code> or <code>Decimal</code>, simple type equality will suffice here.</p> <h3>3. Built-in C# types</h3> <p>If your definition of primitive types is the list of <a href="http://msdn.microsoft.com/en-gb/library/ya5y69ds.aspx" rel="nofollow">Built-in Types Table (C# Reference)</a> on MSDN, we have to exclude <code>IntPtr</code> and <code>UIntPtr</code> because they are not in that list.</p> <pre><code>public static bool IsPrimitiveType(Type type) { return (type.IsPrimitive &amp;&amp; type != typeof(UIntPtr) &amp;&amp; type != typeof(IntPtr)) || type == typeof(decimal) || type == typeof(string); } </code></pre> <h3>4. Something else entirely</h3> <p>Based on the previous examples you can see how to exclude or include additional types in your definition of a <em>primitive type</em> if you want to.</p> <hr> <p>In all the above examples, you can call the <code>IsPrimitiveType</code> method like this:</p> <ol> <li><p>If you have an object instance <code>obj</code>:</p> <pre><code>bool isPrimitive = IsPrimitiveType(obj.GetType()); </code></pre></li> <li><p>If you have a type <code>someType</code>:</p> <pre><code>bool isPrimitive = IsPrimitiveType(someType); </code></pre></li> <li><p>If you have a generic type parameter <code>T</code>:</p> <pre><code>bool isPrimitive = IsPrimitiveType(typeof(T)); </code></pre></li> <li><p>If you have a type known at compile time, e.g. <code>Int32</code>:</p> <pre><code>bool isPrimitive = IsPrimitiveType(typeof(Int32)); </code></pre></li> </ol>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload