Bald Bearded Builder
csharp/stack-vs-heap-in-net

Stack vs. Heap in .NET

Git stash temporarily saves uncommitted changes, restores a clean working directory, and lets you reapply selected work later.

Loads from YouTube when you press play

Transcript 1 topic

Classes versus structs

0:00

When you're talking about whether things are classes or structs in dot net, you're often talking about reference types and value types. And what it really all boils down to is where do things live, on the stack or the heap. Let's talk about what those things are. Our code compiles into commands and those commands run on threads. Each thread has a stack.

A stack is very much like a stack that we think of in real life. Think like a stack of trades. As we add things to the stack, we add one to the top. And if we wanted to access the second one down, we have to pull through to get that one. Value types get added to the stack and things added to a stack can't be accessed outside of that thread.

And the heap is exactly what it sounds like, a pile of things that we can access randomly. Think of it like memory for our app. So think of this stack of boxes as our heap. I can go through and pick whichever ones I want without having to worry about the others in the pile.

Now classes are reference types so they live on the heap and you may have properties that are value types but they're still accessible on the heap because they're part of that class which is on the heap. And that's the basics of the stack versus the heap.

Sign in to join in. Reading needs nothing.