0

Some types are primitive. I can see a definition of what types out of the simple types are primitive and which are not from the MSDN documentation.

What's special about a primitive type other than they're on that list?

| Type           | IsPrimitive | IsValueType |
|----------------|-------------|-------------| 
| System.Boolean | True        | True        |
| System.Byte    | True        | True        |
| System.Char    | True        | True        |
| System.Decimal | False       | True        |
| System.Double  | True        | True        |
| System.Single  | True        | True        |
| System.Int32   | True        | True        |
| System.Int64   | True        | True        |
| System.SByte   | True        | True        |
| System.UInt32  | True        | True        |
| System.UInt64  | True        | True        |
| System.UInt16  | True        | True        |
| System.String  | False       | False       |
| System.Object  | False       | False       |
  • All primitives are value types.
  • Not all simple value types are primitives.
  • Not all CTS value types are primitives.
BanksySan
  • 27,362
  • 33
  • 117
  • 216
  • That it has to be blittable as well ? – aybe Nov 20 '19 at 22:10
  • 1
    https://softwareengineering.stackexchange.com/q/139747/117476 – Ňɏssa Pøngjǣrdenlarp Nov 20 '19 at 22:13
  • 2
    It is a bit arbitrary, neither the CLI nor the C# spec defines the concept. But in general they are types that the processor knows about, if you use them then pref is going to fantastic. Strings, arrays, objects, not fantastic, the processor is clueless about them and has to be steered into doing the right thing with multiple instructions. And yes, `decimal` is the odd duck out. The C# language makes it look like it is primitive but anything you do with it requires a helper call into mscorlib or the CLR. Slow, not primitive. – Hans Passant Nov 20 '19 at 22:50
  • Short version: a .NET or C# type is primitive if the designers say it is. See marked duplicate's second answer for extended discussion on the question. – Peter Duniho Nov 20 '19 at 22:53
  • @Aybe definitely not that because they aren't all blittable (e.g. `Int64` isn't blittable on in a 32-bit environment and isn't _guarenteed_ blittable in any environment). – BanksySan Nov 20 '19 at 23:25
  • @BanksySan Good point, thank you! – aybe Nov 21 '19 at 02:34
  • 1
    _"What's important is that reference types are passed by reference and value types are copied"_ -- I noticed this statement in your now-deleted comment. Unfortunately, as with the heap/stack trope, your statement is also wrong. All parameters are passed by-value, unless otherwise indicated by `ref`, `out`, and `in`. For reference types, the value _is_ a reference, but the parameter is still passed by-value. ... – Peter Duniho Nov 21 '19 at 17:07
  • 1
    ... See e.g. [_"Do not confuse the concept of passing by reference with the concept of reference types. The two concepts are not the same."_](https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/ref#passing-an-argument-by-reference) – Peter Duniho Nov 21 '19 at 17:07
  • @PeterDuniho Good spot. Thanks. – BanksySan Nov 21 '19 at 22:09

0 Answers0