Hi,
Just started learning C#. Trying to figure out the difference between structure and classes in C#.
Most of the documents say that structures are allocated in stack. Is it true? Is there any specific reason
for doing so?
Please clarify
Thanks in advance.
Loading

KrishnarajPosted Jun 5, 2006, 7:36 AM
Thanks. Sorry for the delay in replying. But just trying to figure out why in C# they did it like this? In C++ paradigm structures and classes are treated as same(with some exceptions :-)). Is there any advantage by making structs are value types? and structs are sealed by default.
Stan GershengorenPosted Sep 15, 2005, 1:16 PM
I think (not 100% sure) that structs are allocated in the stack because they are value types,
meaning that you know exactly how much memory is needed for them. When you declare a class,
in reality you just declare a reference (pointer) to an object in heap of that type (class),
and you do not know before compiling, how much memory u will need. (keep in mind that a reference to a base class
can easily point to one of the derived classes -typical late binding).
Hope that helps