Search code examples
c#filesize

Why does C# compile to the same size even if I use a smaller datatype?


I did a little test, because I wanted to see the difference in file size between sbyte and an int, but when I compile the file to .exe, it's 4kb for both. Why is this happening? I was fascinated that by people who make 4kb games in java, but a hello world app is 4kb in c#.


Solution

  • Very generally speaking, program's code tells CPU what to do - how much memory to allocate, what to put in the allocated memory, how to make decisions based on what's in the memory cells, and so on.

    The length of the program does not depend on the amount of memory it allocates at run time, in the same way the two statements

    allocate 100 1-byte units
    

    and

    allocate 500 4-byte units
    

    use the same number of characters.

    The difference comes at run-time, though: the first statement allocates 100 bytes, while the second one allocates 2,000 bytes. This difference does not reflect on the size of the statements describing the allocation.