(edit: My original answer contained the oversimplification "structs are allocated on the stack" and confused stack-vs-heap and value-vs-reference concerns a bit, because they are coupled in C#.)
Whether objects live on the stack or not is an implementation detail which is not very important. Jon has already explained this well. When choosing between using a class and struct, it is more important to realize that reference types work differently than value types. Take the following simple class as an example:
public class Foo
{
public int X = 0;
}
Now consider the following code:
Foo foo = new Foo();
Foo foo2 = foo;
foo2.X = 1;
In this example, foo and foo2 are references to the same object. Setting X on foo2 will also affect foo1. If we change the Foo class to a struct then this is no longer the case. This is because structs are not accessed through references. Assigning foo2 will actually make a copy.
One of the reasons for putting stuff on the stack is that the garbage collector does not have to clean it up. You typically should not worry about such things; just use classes! Modern garbage collectors do a pretty good job. Some modern virtual machines (like java 1.6) can even determine whether it is safe to allocate objects on the stack even if they are not value types.
stack allocation in general
and not specific to .NET. – Gathardobject
stored in the heap) when items are cast as e.g.int
(stored in the stack), then reboxed (e.g. because they are passed asobject
in a method). The cost is significant. It may be reduced by designing variables and method arguments not requiring boxing/unboxing. More. – Minimal