class1.cs

来自「C#开发教程 由浅入深 配有实例 是初学者的好帮手」· CS 代码 · 共 44 行

CS
44
字号
using System;

namespace GarbageDisposalWrong
{
	public class Thing
	{
		private string name;
		public Thing(string name) { this.name = name; }
		override public string ToString() { return name; }
		~Thing() { Console.WriteLine("~Thing()"); }
		public void Dispose()
		{
			Console.WriteLine("Dispose()");
			Another a = new Another();
//			GC.SuppressFinalize(this);
			GC.SuppressFinalize(a);
		}
	}

	public class Another
	{
		~Another() { Console.WriteLine("~Another()"); }
	}

	public class GarbageDisposalWrongApp
	{
		[STAThread]
		public static void Main(string[] args)
		{
			DoSomething();
			Console.WriteLine("end of Main");
		}
		public static void DoSomething()
		{
			Thing t = new Thing("Foo");
			Console.WriteLine(t);
			t.Dispose();
			t = null;
			GC.Collect();
			GC.WaitForPendingFinalizers();
		}
	}
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?