please delete me
please delete me

Reputation:

C#. Where struct methods code kept in memory?

It is somewhat known where .NET keeps value types in memory (mostly in stack but could be in heap in certain circumstances etc)...

My question is - where is the code of the struct?

If I have say 16 byte of data fields in the struct and a massive computation method in it - I am presuming that 16 byte will be copied in stack and the method code is stored somewhere else and is shared for all instances of the struct.

Are these presumptions correct?

Upvotes: 13

Views: 1298

Answers (3)

Mark Dickinson
Mark Dickinson

Reputation: 6633

This is a great article for figuring out what goes where.

Upvotes: 2

Paul Alexander
Paul Alexander

Reputation: 32367

The MSIL is stored in the code section of the assembly - which Windows maps into memory when the assembly is first loaded. When the method is first executed the JIT will compile the MSIL to x86/x64 code. Once the method is compiled into memory it usually stays there and will be shared by all threads. There are some circumstances where multiple AppDomains will cause the MSIL to be compiled a second time, but it's rare.

Upvotes: 5

TomTom
TomTom

Reputation: 62093

Yes. Basically methods are managed separately in some structure that - basically - is not SO well known and documented (as noone ever needs it).

it is also kept as bytecode, compiled, may also be inlined in other methods.

The struct is known by type, so the calls to the methods can be routed properly.

Upvotes: 2

Related Questions