Skip to content
欢迎扫码关注公众号

.NET Core 实战 [No.82] 析构函数

析构函数(destructor)与构造函数(constructor)的作用正好相反。

构造函数在创建对象实例时调用,用于对类型成员初始化;
析构函数在对象实例即将被回收时执行,可用于一些清理工作。

析构函数以 ~ 开头,无返回值,无参数,~ 之后紧跟类名(无空格)。

csharp
class Test {
    ~Test() {

    }
}

示例代码

Test.cs

csharp
using System;

namespace DestructorSample
{
    class Test
    {
        public Test()
        {
            Console.WriteLine("构造函数被调用。");
        }

        ~Test() {
            Console.WriteLine("析构函数被调用。");
        }
    }
}

Program.cs

csharp
using System;

namespace DestructorSample
{
    class Program
    {
        static void Main(string[] args)
        {
            Test();
            GC.Collect(); // 垃圾回收
            Console.ReadLine();
        }

        static void Test()
        {
            var test = new Test();
        }
    }
}

参考:《.NET Core 实战:手把手教你掌握 380 个精彩案例》 -- 周家安 著


Page Layout Max Width

Adjust the exact value of the page width of VitePress layout to adapt to different reading needs and screens.

Adjust the maximum width of the page layout
A ranged slider for user to choose and customize their desired width of the maximum width of the page layout can go.

Content Layout Max Width

Adjust the exact value of the document content width of VitePress layout to adapt to different reading needs and screens.

Adjust the maximum width of the content layout
A ranged slider for user to choose and customize their desired width of the maximum width of the content layout can go.