目录
C# 中析构函数的属性
析构函数在 C# 中如何工作?
示例#4
结论
 推荐文章
首页 后端开发 C#.Net教程 C# 中的析构函数

C# 中的析构函数

Sep 03, 2024 pm 03:12 PM
c# c# tutorial

在 C# 中的析构函数一文中,顾名思义,析构函数是 C# 中销毁对象的方法。如果不再需要这些对象,则调用析构函数以从类中销毁这些对象。析构函数将由垃圾收集器自动调用并销毁对象。

语法:

class Demo
{
// other methods
~Demo()  // destructor
{
// your code
}
}
登录后复制

C# 析构函数是 Finalize( ) 方法的快捷方式。所以当你声明析构函数时

~Demo()   // destructor
{
// your code
}
登录后复制

C# 编译器会将其翻译为:

protected override void Finalize()
{
try
{
// your code
}
finally
{
base.Finalize();
}
}
登录后复制

析构函数由 ~(波形符)表示。

C# 中析构函数的属性

以下是析构函数的属性:

  1. 析构函数不能有任何参数和访问修饰符。
  2. 每个类只能包含一个析构函数。
  3. 析构函数不能重载或继承。
  4. 析构函数名称始终与类名称相同,并且没有返回类型。
  5. 析构函数使用 Finalize 方法,并在不再需要对象时由垃圾收集器调用。
  6. 析构函数遵循相反的模式。在析构函数中,首先调用派生类,然后调用基类。

析构函数在 C# 中如何工作?

这里有一些示例,展示了它在 C# 中的工作原理。

示例#1

代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Destructor
{
class person
{
//variables
public string name;
public int age;
public person(string name,int age)   //parametrized constructor
{
this.name = name;
this.age = age;
}
public string getName()
{
return this.name;
}
public int getAge()
{
return this.age;
}
~person()      // destructor
{
Console.WriteLine("Destructor has been invoked");
}
}
class Program
{
// main method
static void Main(string[] args)
{
person Details = new person("Joe", 28);
Console.WriteLine(Details.getName());
Console.WriteLine(Details.getAge());
}
}
}
登录后复制

在上面的示例中,参数化构造函数使用参数名称和年龄进行初始化,其中这是引用类变量的关键字。之后,使用与类名和符号相同的名称创建析构函数〜。在main方法中,有一个person类的对象。获得一个人的姓名和年龄后,就不再需要对象了。因此,析构函数被调用,它会销毁对象并取消分配它们的内存。

输出:

C# 中的析构函数

示例#2

代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
anmespace Destructor
{
class person
{
// variables
public string name;
public int age;
public person(string name,int age) // parameterized constructor
{
this.name = name;
this.age = age;
}
public string getName()
{
return this.name;
}
public int getAge()
{
return this.age;
}
~person()     //destructor
{
Console.WriteLine("Descructor has been invoked");
}
}
class Program
{
// Main method
static void Main(string[] args)
{
person Details = new person("Joe", 28);    // first object
person Details1 = new person("John", 20);
Console.WriteLine(Details.getName());
Console.WriteLine(Details.getAge());
Console.WriteLine(Details1.getName());
Console.WriteLine(Details1.getAge());
}
}
}
登录后复制

这个例子和前面的例子几乎一样,但是在这个例子中,main方法中有两个对象。众所周知,构造函数为每个对象运行,同样的事情也适用于析构函数。在这种情况下,析构函数被调用两次并取消分配每个对象的内存。

输出:

C# 中的析构函数

示例#3

代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Destructor
{
public class Parent
{
~Parent()   // base destructor
{
Console.WriteLine("Parent.~Parent()");
}
}
public class Child : Parent
{
~Child()  // derived destructor
{
Console.WriteLine("Child.~Child()");
}
}
public class MainClass
{
static void Main()
{
Child child = new Child();
}
}
}
登录后复制

在上面的示例中,定义了具有析构函数的父类。然后子类继承父类并且也包含析构函数。所以子析构函数会自动调用基析构函数。

在构造函数中,首先调用基本构造函数。例如,如果我们有基类 A,它被类 B 继承,那么在构造函数的情况下,首先调用类 A,然后调用类 B。但是,在析构函数的情况下,首先调用类 B(派生类),然后再调用类 A(基类)。

订单执行的另一个例子:-

代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Destructor
{
class Tree
{
~Tree()
{
System.Console.WriteLine("This is the first destructor");
}
}
class Branch: Tree
{
~Branch()
{
System.Console.WriteLine("This is the second destructor");
}
}
class Flower: Branch
{
~Flower()
{
System.Console.WriteLine("This is the third destructor");
}
}
class Test
{
static void Main()
{
Flower f= new Flower();
}
}
}
登录后复制

输出:

C# 中的析构函数

如您所见,首先调用第三个构造函数,然后调用第二个和第一个构造函数。

示例#4

代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Destructor
{
class Example
{
public Example()
{
// constructor
Console.WriteLine("Object Created");
}
// Destructor
~Example()
{
Console.WriteLine("Object Destroyed");
}
}
class Program
{
public static void Sample()
{
Example ex = new Example();
}
static void Main(string[] args)
{
Sample();
GC.Collect();
Console.ReadLine();
}
}
}
登录后复制

输出:

C# 中的析构函数

如果程序结束时不需要对象的内存,则析构函数会取消分配对象的内存。但有时如果我们在程序执行过程中使用GC.Collect(),它会在中间销毁对象并释放这些对象的内存。析构函数可以隐式或显式调用。但无需显式销毁对象,因为 C# 提供垃圾收集。但是,当您使用完非托管资源后,您将需要显式释放它们。无需调用或管理资源的情况。使用析构函数来处理非托管资源。垃圾收集器将调用析构函数,因为它由具有析构函数的对象列表组成。因此,每当创建或销毁对象时,该列表就会更新。如果队列中有对象,则在析构函数执行后,它会被垃圾收集器收集。

结论

析构函数的主要目的是在对象执行后释放其内存。因此,析构函数中执行了不同的操作,例如回收空间、释放网络资源和资源锁等。析构函数应该用于释放非托管资源而不是托管资源。

 推荐文章

这是 C# 中析构函数的指南。这里我们讨论C#中析构函数的介绍、属性以及示例。 您还可以阅读我们其他推荐的文章以了解更多信息 –

  1. Java 中的析构函数
  2. C# 中的继承
  3. C# 中的复制构造函数
  4. Python 中的析构函数

以上是C# 中的析构函数的详细内容。更多信息请关注PHP中文网其他相关文章!

本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn

热AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover

AI Clothes Remover

用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool

Undress AI Tool

免费脱衣服图片

Clothoff.io

Clothoff.io

AI脱衣机

AI Hentai Generator

AI Hentai Generator

免费生成ai无尽的。

热门文章

R.E.P.O.能量晶体解释及其做什么(黄色晶体)
2 周前 By 尊渡假赌尊渡假赌尊渡假赌
仓库:如何复兴队友
4 周前 By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island冒险:如何获得巨型种子
3 周前 By 尊渡假赌尊渡假赌尊渡假赌

热工具

记事本++7.3.1

记事本++7.3.1

好用且免费的代码编辑器

SublimeText3汉化版

SublimeText3汉化版

中文版,非常好用

禅工作室 13.0.1

禅工作室 13.0.1

功能强大的PHP集成开发环境

Dreamweaver CS6

Dreamweaver CS6

视觉化网页开发工具

SublimeText3 Mac版

SublimeText3 Mac版

神级代码编辑软件(SublimeText3)

使用 C# 的活动目录 使用 C# 的活动目录 Sep 03, 2024 pm 03:33 PM

使用 C# 的活动目录

C# 中的访问修饰符 C# 中的访问修饰符 Sep 03, 2024 pm 03:24 PM

C# 中的访问修饰符

C# 中的随机数生成器 C# 中的随机数生成器 Sep 03, 2024 pm 03:34 PM

C# 中的随机数生成器

C# 数据网格视图 C# 数据网格视图 Sep 03, 2024 pm 03:32 PM

C# 数据网格视图

C# 字符串读取器 C# 字符串读取器 Sep 03, 2024 pm 03:23 PM

C# 字符串读取器

C# 中的模式 C# 中的模式 Sep 03, 2024 pm 03:33 PM

C# 中的模式

C# 字符串编写器 C# 字符串编写器 Sep 03, 2024 pm 03:23 PM

C# 字符串编写器

C# 中的二进制编写器 C# 中的二进制编写器 Sep 03, 2024 pm 03:22 PM

C# 中的二进制编写器

See all articles