1.in classes ,why all the objects are stored only at stack and all the values are stored at heap why not viceversa
2.do static constructors can have parameters if not why
Know the answer? Post it. Somebody with the same question will find it here.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend4days leftRegister
VulpesRank #55 · 29.2K reputationPosted Nov 6, 2014, 6:01 AM EST
Values (i.e. instances of structs or enums) may be stored on the stack or on the heap.
It was just a design decision that objects should always be stored on the heap. In other languages (such as C++) they can be allocated on the stack as well.
I don't know whether the reasons for this have ever been published but I'd imagine that the following would be factors in the decision:
1. Objects (but not values) are subject to garbage collection and having all objects in heap memory is more convenient for the GC process.
2. Objects usually require more memory than structs or enums, as well as some additional overhead. Given the size of the stack is limited (1 MB by default) but the heap is only limited by available memory, there will be fewer memory allocation problems if objects are always allocated on the heap.
Static constructors cannot have any parameters. This is because they are always called automatically by the system before any other static or instance member of the class or struct is invoked. Consequently there is no way that the system would know what arguments were to be passed for those parameters if a static constructor were allowed to have them.