.Net / C #: what is the real size of the integer?

in .Net, integers are signs, which means they are stored on the stack. Integers are also classes (usually System.Int32). They have methods like CompareTo, Equals, ... Thus, they must accept more than four bytes on the stack. The example below shows that they take exactly 4 bytes:

unsafe static void Main()
{
    int a = 2, b = 4;
    Console.WriteLine("Adress of a : {0}", (int)&a);
    Console.WriteLine("Adress of b : {0}", (int)&b);
    Console.WriteLine("Size of integer: {0}", (int)(&a) - (int)(&b));
}

Output:

Adress of a : 1372876
Adress of b : 1372872
Size of integer: 4

Is the CLR a special treatment for integers and other values ​​(float, long, double, ...)?

+3
source share
4 answers

No, the fact that they are value types does not mean that they are stored on the stack. This means that they are stored wherever a variable exists .

, , ( ..) . 4 . ? vtable , : , ..

EDIT: ( ), System.Int32 - , . ( , CLR , int, .)

+15

, .

. . , "" .

, , .NET Object.

+5

, . , .

, . , , . Int32 .

, ( ). , , , . , , . , , StringBuilder:

object o = new StringBuilder();

- , (4 32- ). StringBuilder , , .

, . , Object:

object p = 42;

. , , 12 ( 32- ).

+4

, , ...

// type definition
public class Bla {}
// instance of type bla
public Bla myBla = new Bla();

int (4 ), , , , .

, , CompareTo, , , , , 0 .

0
source

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


All Articles