কম্পিউটার টিউটোরিয়াল

C#-এ ডিস্ট্রাক্টর (Destructor) কী?

একটি ডিস্ট্রাক্টর (Destructor) হলো একটি ক্লাসের স্পেশাল মেম্বার ফাংশন যা যখনই তার ক্লাসের কোনো অবজেক্ট স্কোপ বাইরে চলে যায়, তখনি এক্সিকিউট হয়।

এটির নাম ক্লাসের নামের সাথে একই থাকে কিন্তু শুরুতে টিল্ডা (~) থাকে। উদাহরণস্বরূপ, আমাদের ক্লাসের নাম Demo।

public Demo() { // constructor 
    Console.WriteLine("Object is being created"); 
} 
~Demo() { // destructor 
    Console.WriteLine("Object is being deleted"); 
}

উদাহরণ

using System; 
namespace LineApplication { 
    class Line { 
        private double length; 
        public Line() { // constructor 
            Console.WriteLine("Object is being created"); 
        } 
        ~Line() { // destructor 
            Console.WriteLine("Object is being deleted"); 
        } 
        public void setLength(double len) { length = len; } 
        public double getLength() { return length; } 
        static void Main(string[] args) { 
            Line line = new Line(); 
            line.setLength(6.0); 
            Console.WriteLine("Length of line : {0}", line.getLength()); 
        } 
    } 
}

আউটপুট

Object is being created
Length of line : 6
Object is being deleted
  1. C#-এ নেমস্পেস (Namespaces) কী?

  2. C#-এ কনস্ট্রাক্টর (Constructors) এবং ডিস্ট্রাক্টর (Destructors) এর পার্থক্য

  3. C# প্রোগ্রামে কনস্ট্রাক্টর (Constructors) কী?

  4. C#-এ রিলেশনাল অপারেটর (Relational Operators) কী কী?