学习笔记:C#构造函数和析构函数

参考书目:C#6.0学习笔记——从第一行C#代码到第一个项目设计(作者周家安) P56


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using static System.Console;
namespace MyApp
{
    public class Test
    {
        // 构造函数
        public Test()
        {
            System.Diagnostics.Debug.WriteLine("构造函数被调用。");
            WriteLine("构造函数被调用。");
        }

        // 析构函数
        ~Test()
        {
            System.Diagnostics.Debug.WriteLine("析构函数被调用。");
            WriteLine("析构函数被调用。");
        }
    }
    class Program
    {
        static void Main(string[] args)
        {
            Test t = new Test();
            ReadLine();
        }
    }
}

运行结果:

发布了33 篇原创文章 · 获赞 1 · 访问量 691

猜你喜欢

转载自blog.csdn.net/qq_41708281/article/details/104231318