示例1
class Program
{
static void Main()
{
TEST();
}
static void TEST()
{
string stringTest = "somestring"; //this value stored in heap part of the memory becase its a object type not a primitive type.
int first = 5; //this one stroed in stack part because its a primitive type.
int second = first; //but this one includes a reference, so this will be stored in heap part ???
}
}
示例2
class testObject
{
}
class Program
{
static void Main()
{
var first = new testObject(); // newly created testObject stored in heap because it's a class which is an object and includes a reference, and "first" value stored in stack because it is just a reference.
new testObject();// but this one doesnt include a reference so is this still stored in the heap?
}
}
那么,对于堆中的一个值,只需要一个引用?还是按照对象的类型出现的每一个对象也都分配到了堆上?
嗯,我做了一些更多的研究,据我了解,当您创建一个新对象时,即使该对象不包含引用,它也会被放入堆中。我认为 YouTube 视频和一些在线课程中的解释不正确:-D