【发布时间】:2016-11-17 01:07:19
【问题描述】:
假设我创建了一个对象:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace CreateObjOnobj
{
class Program
{
static void Main(string[] args)
{
testcreate myobjecttotest;
myobjecttotest = new testcreate();
myobjecttotest.num = 20;
myobjecttotest.bill = true;
myobjecttotest = new testcreate();
Console.WriteLine(myobjecttotest.bill.ToString());
Console.ReadKey();
}
}
class testcreate
{
public int num = 0;
public bool bill = false;
}
}
此代码是否会自动删除对象并创建一个新对象而不会丢失内存?
谢谢
【问题讨论】:
-
是的。这就是垃圾收集语言(如 Java 和 C#)所做的 :) 您创建了第二个“myobjecttotest”,第一个可用于垃圾收集。
标签: c# object constructor instance destructor