Why does C # implement an integer type as a structure, and not as a primitive type?

Looking at how the type is intimplemented as System.Int32 structin C # , I wonder why it was implemented as wrapping it in a structure type) and did not choose the implementation of the primitive type, as in Java ?

Is additional packaging added to the structure of additional performance views?

+4
source share
1 answer

Not here at struct. For all practical purposes, it System.Int32 structis a built-in primitive type, in the sense that the compiler recognizes them and generates special instructions when processing expressions of primitive types. The only place where int(or any other struct, for that matter) is wrapped during the box conversion, which is required when you want to pass intto the API that accepts the object.

The biggest difference between handling Java and C # primitives is that you can use C # primitives where custom types can be used struct, especially in general C # arguments, while Java treats primitives as a completely separate group of types.

+5
source

Source: https://habr.com/ru/post/1662133/


All Articles