C #: Etudes, part 4

    Continuation, the previous part is here.

    So, today we are using a hammer and a shovel to try to make a craniotomy to fit into the inside of .NET.

    There is a simple class: class X {public int Val; }. When creating an object of this class, what real size will it occupy? And what values ​​will the additional fields contain? Present the proof in the form of code in C #;)

    Of course, this is only the first riddle of the series, but this should be enough at least for the weekend. Finding out the internals of .NET is a pretty big topic.
    Note: since we are talking about system-dependent things, I will assume that the code runs on a 32-bit OS.

    UPD So, we got two similar solutions: from lam0x86: iaroshenko.habrahabr.ru/blog/77275/#comment_2250121 and from crjd : iaroshenko.habrahabr.ru/blog/77275/#comment_2252317 . I first classified them as incorrect, but later I saw my mistake.

    However, both solutions are based on the fact that the class has an open field int. If this is not a field, but a property, or there will be no fields or properties at all, then this solution will not work.
    Therefore, I decided to demonstrate the "author's version": iaroshenko.habrahabr.ru/blog/77275/#comment_2269072 . Naturally, it will work only in this limited framework: the simplest program, where there is no need to create other objects or collect garbage.

    By the way, if you remove the fields altogether, then the size still remains the same, only the additional field is zero.

    Also popular now: