⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 class1.cs

📁 C#开发教程 由浅入深 配有实例 是初学者的好帮手
💻 CS
字号:
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 + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -