object creation in stack

.net, c#, memory-management, object

Solution

.NET reference types always live on the GC heap. It's not possible to have them elsewhere. Even C++/CLI that supports things like

System::Random rnd;
rnd.Next();

that looks like stack variables, actually creates the `Random` class, on the GC heap (and calls its `Dispose` method at the end of the block if it implements `IDisposable`.)

That said, as Eric Lippert says, the stack is an implementation detail and you should primarily care about reference or value semantics of the types you create.

Problem

Can I create an object of my class in stack regarding .net and C#? For example: ``` class abc { int i=1; } abc a1=new abc(); ``` Now, here the object is created in heap. So, is there any way to create the object in stack memory of ram if the size of object is not big?

Original source