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.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
VulpesPosted Nov 6, 2014, 11:01 AM
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.