class1.cs

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

CS
53
字号
using System;

namespace TestUsing
{
    public class Thing : IDisposable
    {
        private string name;
        public Thing(string name) { this.name = name; }
        override public string ToString() 
        { return String.Format("Thing : " +name); }
 
        ~Thing() 
        { 
            Dispose();
            Console.WriteLine("~Thing: " +name); 
        }
 
        public void Dispose()
        {
            Console.WriteLine("Dispose: " +name);
            GC.SuppressFinalize(this);
        }
    }

    class TestUsingApp
    {
        [STAThread]
        static void Main(string[] args)
        {
            Thing t1 = new Thing("Ethel");
            try
            {
                Console.WriteLine(t1);
            }
            finally
            {
                if (t1 != null)
                    ((IDisposable)t1).Dispose();
            }
 
            using (Thing t2 = new Thing("JimBob")) 
            {
                Console.WriteLine(t2);
            }

            Console.WriteLine("\nForcing a Collection");
 
            GC.Collect();
            GC.WaitForPendingFinalizers();
        }
    }
}

⌨️ 快捷键说明

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