C# 中的析構函數

WBOY
發布: 2024-09-03 15:12:58
原創
936 人瀏覽過

在 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中文網其他相關文章!

相關標籤:
來源:php
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!