Click here to Skip to main content
15,897,226 members
Articles / Programming Languages / C#

Non-Deterministic Destructors - What Are They?

Rate me:
Please Sign up or sign in to vote.
2.97/5 (12 votes)
31 Jul 2007CPOL4 min read 54.9K   12  
An article on non-deterministic destructors in C#
namespace Example.Destructors
{
    partial class MainForm
    {
        /// <summary>
        /// Required designer variable.
        /// </summary>
        private System.ComponentModel.IContainer components = null;

        /// <summary>
        /// Clean up any resources being used.
        /// </summary>
        /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }

        #region Windows Form Designer generated code

        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            this.btnUsingDispose = new System.Windows.Forms.Button();
            this.btnInsideUsing = new System.Windows.Forms.Button();
            this.btnNotUsingDispose = new System.Windows.Forms.Button();
            this.btnGarbageCollect = new System.Windows.Forms.Button();
            this.label1 = new System.Windows.Forms.Label();
            this.SuspendLayout();
            // 
            // btnUsingDispose
            // 
            this.btnUsingDispose.Location = new System.Drawing.Point(12, 12);
            this.btnUsingDispose.Name = "btnUsingDispose";
            this.btnUsingDispose.Size = new System.Drawing.Size(202, 73);
            this.btnUsingDispose.TabIndex = 0;
            this.btnUsingDispose.Text = "Using Dispose()";
            this.btnUsingDispose.UseVisualStyleBackColor = true;
            this.btnUsingDispose.Click += new System.EventHandler(this.btnUsingDispose_Click);
            // 
            // btnInsideUsing
            // 
            this.btnInsideUsing.Location = new System.Drawing.Point(12, 111);
            this.btnInsideUsing.Name = "btnInsideUsing";
            this.btnInsideUsing.Size = new System.Drawing.Size(202, 73);
            this.btnInsideUsing.TabIndex = 0;
            this.btnInsideUsing.Text = "Inside using";
            this.btnInsideUsing.UseVisualStyleBackColor = true;
            this.btnInsideUsing.Click += new System.EventHandler(this.btnInsideUsing_Click);
            // 
            // btnNotUsingDispose
            // 
            this.btnNotUsingDispose.Location = new System.Drawing.Point(12, 203);
            this.btnNotUsingDispose.Name = "btnNotUsingDispose";
            this.btnNotUsingDispose.Size = new System.Drawing.Size(202, 73);
            this.btnNotUsingDispose.TabIndex = 0;
            this.btnNotUsingDispose.Text = "Not Using Dispose()";
            this.btnNotUsingDispose.UseVisualStyleBackColor = true;
            this.btnNotUsingDispose.Click += new System.EventHandler(this.btnNotUsingDispose_Click);
            // 
            // btnGarbageCollect
            // 
            this.btnGarbageCollect.Location = new System.Drawing.Point(299, 203);
            this.btnGarbageCollect.Name = "btnGarbageCollect";
            this.btnGarbageCollect.Size = new System.Drawing.Size(134, 73);
            this.btnGarbageCollect.TabIndex = 0;
            this.btnGarbageCollect.Text = "GC.Collect()";
            this.btnGarbageCollect.UseVisualStyleBackColor = true;
            this.btnGarbageCollect.Click += new System.EventHandler(this.btnGarbageCollect_Click);
            // 
            // label1
            // 
            this.label1.AutoSize = true;
            this.label1.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.label1.Location = new System.Drawing.Point(237, 233);
            this.label1.Name = "label1";
            this.label1.Size = new System.Drawing.Size(37, 13);
            this.label1.TabIndex = 1;
            this.label1.Text = " AND";
            // 
            // MainForm
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(454, 308);
            this.Controls.Add(this.label1);
            this.Controls.Add(this.btnGarbageCollect);
            this.Controls.Add(this.btnNotUsingDispose);
            this.Controls.Add(this.btnInsideUsing);
            this.Controls.Add(this.btnUsingDispose);
            this.Name = "MainForm";
            this.Text = "Non-Deterministic Destructors";
            this.ResumeLayout(false);
            this.PerformLayout();

        }

        #endregion

        private System.Windows.Forms.Button btnUsingDispose;
        private System.Windows.Forms.Button btnInsideUsing;
        private System.Windows.Forms.Button btnNotUsingDispose;
        private System.Windows.Forms.Button btnGarbageCollect;
        private System.Windows.Forms.Label label1;
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Architect
United States United States
Jean Paul is a Microsoft MVP and Architect with 12+ years of experience. He is very much passionate in programming and his core skills are SharePoint, ASP.NET & C#.

In the academic side he do hold a BS in Computer Science & MBA. In the certification side he holds MCPD & MCTS spanning from .Net Fundamentals to SQL Server.

Most of the free time he will be doing technical activities like researching solutions, writing articles, resolving forum problems etc. He believes quality & satisfaction goes hand in hand.

You can find some of his work over here. He blogs at http://jeanpaulva.com

Comments and Discussions