7

Looking on how the int type is implemented as the System.Int32 struct in C#, I wonder why was implemented like this (by wrapping this into a struct type), and not opted for a primitive type implementation like in Java?

Doesn't the additional wrapping in a struct produces additional performance hits?

Tunaki
  • 132,869
  • 46
  • 340
  • 423
Tamas Ionut
  • 4,240
  • 5
  • 36
  • 59
  • Possible duplicate of [Why System.Int32 is struct and System.String is class](http://stackoverflow.com/questions/21021628/why-system-int32-is-struct-and-system-string-is-class) – Franck Nov 28 '16 at 13:04
  • Yes, but is implemented as a struct type. My question is why there isn't direct support for true native "primitive" types in NET? – Tamas Ionut Nov 28 '16 at 13:05
  • Possible duplicate of [How are the "primitive" types defined non-recursively?](http://stackoverflow.com/questions/4751885/how-are-the-primitive-types-defined-non-recursively) – M.kazem Akhgary Nov 28 '16 at 13:08
  • For convenience: you can put, say, `List list`, `123.ToString();` etc. Occam raisor: integer is a class, not a special *primitive type* – Dmitry Bychenko Nov 28 '16 at 13:08
  • C# treats all datatypes as classes or structs which have their own methods. – hecate Nov 28 '16 at 13:10

1 Answers1

8

There is no "wrapping" in a struct going on here. For all practical purposes, System.Int32 struct is a built-in primitive type, in the sense that the compiler recognizes them, and generates special instructions while handling expressions of primitive types. The only place where an int (or any other struct for that matter) is wrapped is during a boxing conversion, which is required when you want to pass an int to an API that takes an object.

The biggest difference between Java's and C#'s handling of primitives is that you can use C# primitives in places where user-defined struct types can go, most notably, in C# generic arguments, while Java treats primitives as a completely separate group of types.

Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
  • Yes, but in Java, there is direct support for primitive types. Indeed, it's very practical that we can use "int" as generic types, but I was wondering if there could be direct native primitive type support without the additional struct wrapping. – Tamas Ionut Nov 28 '16 at 13:08
  • 3
    @TamasIonut: there is no "wrapping". I guess you're referring to the overhead from [boxing/unboxing](https://msdn.microsoft.com/en-us/library/yz2be5wk.aspx) an `int`. But that happens only if you cast the `int` to `object`. – Tim Schmelter Nov 28 '16 at 13:10
  • Nope, I mean creating a new struct "thing" which I guess produces "more" overhead than how the native primitive type (bytes) are stored in memory. – Tamas Ionut Nov 28 '16 at 13:12
  • 1
    That struct is not same as normal structs you create. you can not create such struct like `int` that has `int` field in it self. that's special case. @TamasIonut – M.kazem Akhgary Nov 28 '16 at 13:19
  • @TamasIonut: A value type in memory needs exactly as much memory as its fields need. Also, since value types cannot be inherited, you cannot have virtual methods and the CLR automatically handles instance methods like you'd expect. And it doesn't come at a cost per instance. – Joey Nov 28 '16 at 13:19
  • 2
    I see, got it. So is a special type of struct, handled by the CLR. – Tamas Ionut Nov 28 '16 at 13:19
  • 2
    @TamasIonut None of this "more overhead" is going on - `struct` is just a common name for all value types, including primitives. The compiler knows enough things about primitives to generates different CLR code when it sees them, as opposed to user-defined `struct`s. For example, the compiler takes full advantage of CIL support for built-in types. – Sergey Kalinichenko Nov 28 '16 at 13:21
  • Okay, it will be interesting to see how it performs against Java-based int usage (under various "int-intensive" applications). – Tamas Ionut Nov 28 '16 at 13:33
  • 1
    @M.kazemAkhgary The only thing that's special about `int` is that there are compile time literals for it. Other than that, you could implement it as a user-defined `struct`. The literal constants are the only thing that there's special compiler support for. – Servy Nov 28 '16 at 18:13