Thursday, June 23, 2016

string is basically a fixed size array of char and it is immutable

That is how it can live on the stack instead of the heap in C#. A string technically never grows. There may be other places in this blog where I imply strings live in the heap and I'm just wrong. I'm sorry. Ok, wait, here is a better explanation. A string is a class and a reference type that lives on the heap and has a pointer, but it defers to its guts which is a char array on the stack. As it is immutable, whenever you change it you are remaking its guts on the stack too. So basically, it's fair to just think of a string a reference type on the heap as you will interact with it that way. How it behaves on the stack is all stuff being wrapped and obfuscated from your perspective code monkey.

No comments:

Post a Comment