📜  C#中的析构函数

📅  最后修改于: 2021-05-29 21:25:44             🧑  作者: Mango

C#中的析构函数是类中的方法,用于在不再需要时销毁该类的实例。 .NET Framework的垃圾回收器隐式调用了析构函数,因此程序员无法控制何时调用析构函数。当实例变量或对象不再可访问时,有资格销毁它。

重要事项:

  • 析构函数在其类中是唯一的,即,一个类中不能有多个析构函数。
  • 析构函数没有返回类型,并且具有与类名完全相同的名称(包括相同的大小写)。
  • 它的名称与Tilde符号(〜)前缀相同,因此与构造函数有所区别。
  • 析构函数不接受任何参数和修饰符。
  • 不能在结构中定义。它仅与类一起使用。
  • 它不能被重载或继承。
  • 程序退出时调用。
  • 在内部,Destructor在对象的基类上调用了Finalize方法。

句法:

class Example
{ 
    // Rest of the class
    // members and methods.

   // Destructor
   ~Example()
    {
        // Your code
    }

} 

范例1:

// C# Program to illustrate how 
// a destructor works
using System;
  
namespace GeeksforGeeks {
      
class Complex {
      
    // Class members, private 
    // by default
    int real, img;
  
    // Defining the constructor
    public Complex()
    {
        real = 0;
        img = 0;
    }
  
    // SetValue method sets 
    // value of real and img
    public void SetValue(int r, int i)
    {
        real = r;
        img = i;
    }
  
    // DisplayValue displays 
    // values of real and img
    public void DisplayValue()
    {
        Console.WriteLine("Real = " + real);
        Console.WriteLine("Imaginary = " + img);
    }
  
    // Defining the destructor
    // for class Complex
    ~Complex()
    {
        Console.WriteLine("Destructor was called");
    }
      
} // End class Complex
  
  
// Driver Class
class Program {
      
    // Main Method
    static void Main(string[] args)
    {
          
        // Creating an instance of class 
        // Complex C invokes constructor
        Complex C = new Complex();
  
        // Calling SetValue method using
        // instance C Setting values of 
        // real to 2 and img to 3
        C.SetValue(2, 3);
  
        // Displaying values of real
        // and imaginary parts
        C.DisplayValue();
  
        // Instance is no longer needed
        // Destructor will be called
          
    } // End Main
      
} // End class Program
  
}
输出:
Real = 2
Imaginary = 3
Destructor was called

说明:在上面的示例中,该类由构造函数Complex() ,用于设置复杂类实例的值的SetValue方法,用于显示实例值的DisplayValue方法以及用于销毁实例的Destructor〜Complex ()组成。当不再需要该实例时,该对象将打印消息“调用了析构函数”,这取决于程序员他/她想显示什么消息,或者也可以将其留为空白。

范例2:

// C# Program to illustrate how 
// a destructor works
using System;
using System.IO;
  
namespace GeeksforGeeks {
      
public class Vect {
      
    // Class members, private
    // by default
    double i, j, k;
  
    // Defining the constructor
    public Vect()
    {
        i = 0.0;
        j = 0.0;
        k = 0.0;
        Console.WriteLine("An instance of "+
                      "Vect class created");
    }
  
    // SetVector method sets 
    // the value of i, j, k
    public void SetVector(double iComponent,
                          double jComponent, 
                          double kComponent)
    {
        i = iComponent;
        j = jComponent;
        k = kComponent;
    }
  
    // FindMagnitude calculates the 
    // value of the vector's magnitude
    public double FindMagnitude()
    {
        double m = 0.0;
        m = Math.Sqrt(i * i + j * j + k * k);
        return m;
    }
  
    // Defining the Destructor
    // for class Vect
    ~Vect()
    {
        Console.WriteLine("The instance of"+
                   " Vect class Destroyed");
    }
      
} // End class Vect
  
  
// Driver Class
class Procedure {
      
    // Main Method
    static void Main(string[] args)
    {
        // Creates an instance of Vect 
        // class Calls the constructor
        Vect V1 = new Vect();
  
        // SetVector method is called 
        // with values 2.3, 1.5 and 7.0
        V1.SetVector(2.3, -1.5, 7.0);
  
        // Prints value of magnitude of 
        // vector calculated by the 
        // FindMagnitude method
        Console.WriteLine("Magnitude of the vector "+
            "2.3i-1.5j+7k is " + V1.FindMagnitude());
  
        // Instance is destroyed 
        // since no longer needed
          
    } // End Main
      
} // End class Procedure
  
}
输出:
An instance of Vect class created
Magnitude of the vector 2.3i-1.5j+7k is 7.51930847884298
The instance of Vect class Destroyed

说明:在此示例中,我们有Vect类,它表示矢量的物理量。该类由三个组成部分ijk组成。类的方法包括构造的Vect(),该套到零的所有组件,一个SetVector方法来设置实例的值,一个FindMagnitude方法来计算与它被称为实例的幅度和析构函数〜的Vect( ) ,将在不再需要时销毁实例V1。