Destructor in C #

xiaoxiao2021-03-17  179

Destructor in C #

introduction

It is most important to develop world, performance, flexibility, and security in corporate applications. As a VC programmer, I started my career, and I was transferred to the Web Development Department in a sunny morning. Like each C programmer, I am also very lost. I think each like Tom, Dick or even Harry can be programmed with HTML. However, soon I found that the real challenge is to produce high performance, flexible applications. In summary, the web environment is loosely coupled, and the nature of the country will make you god forever.

In order to make high performance flexible applications, it is critical to use your resources in the most optimized way. A tip is to use your resources as soon as possible and release it as soon as possible. The intention here is to describe the object clearance mechanism in C #.

Deconstructor

We know that 'decchor' is used to clear the cases. When we use the decchor in C #, we must remember the following:

A class can only have a decchor.

The decchor cannot be inherited or overloaded.

The decchor cannot be called. They are automatically called (compiler).

The decchor cannot with a modification or parameter.

Below is a statement of the class myclass decchor: ~ myclass ()

{

// Cleaning Up Code Goes Here

PROTECTED OVERRIDE VOID FINALIZE ()

{

Try

{

// Cleaning Up.

}

Finally

{

Base.Finalize ();

}} Now let's take a look at how the decchor is called. We have three classes A, B and C. B is derived from A, C derived from B. Each class has their own constructor and deconstruction. In the main function of class app, we create C objects. Using system;

Class A

{

Public a ()

{

Console.WriteLine ("CREATING A");

}

~ A ()

{

Console.writeline ("Destroying A");

}

}

Class B: a

{

Public b ()

{

Console.writeline ("CREATING B");

}

~ B ()

{

Console.writeline ("Destroying B");

}

}

Class C: B

{

Public C ()

{

Console.WriteLine ("CREATING C");

}

~ C ()

{

Console.writeline ("Destroying C");

}

}

Class app

{

Public static void main ()

{

C c = new c ();

Console.writeline ("Object Created");

Console.writeline ("Press Enter to Destroy IT);

Console.readline ();

C = NULL;

//Gc.collect ();

Console.read ();

}

}

As we expect, the base class constructor will be executed and the program will wait for the user to press 'Enter'. When this happens, we set the object of class C as null. But the decchor is not executed ..! ! ? ? As we said, the programmer cannot control the decchor to be executed because it is determined by the garbage collection device. But the deductor is called when the program exits. You can check this by redirecing the O / P to text files. I output it here. It is noted that the decchor of the base class is called because the base.finalize () is called behind. Creating a CREATING B CREATING C Object Created Press Enter to Destroy It Destroying C Destroying B Destroying A So, if you use the object you want to call the decchor, what should you do? There are two ways: implementing the IDisposable interface IDisposable interface includes only one public method, which is declared as Void Dispose (). We can implement this method to close or release unmanaged resources such as files, streams, and handles controlled by this interface. This method is used to release the resource of all task joint objects. When this method is implemented, the object must seek to ensure that all the resources associated with the resource inheritors are also released (can't grasp, can't be turned out). Class myclass: idisposable {

Public void dispose ()

{

// Implementation

}

} When we implement the IDisposable interface, we need rules to make sure Dispose is appropriately called.

Here, DISPOSE (BOOL) is overloaded here, and all cleaning code is only written in this method. This method is called by detail and idisposable.dispose (). We should pay attention to Dispose (bool) does not have been called anywhere in addition to in the idisposable.dispose () and demolmers. Using (MyClass Objcls = new myclass ())

{

}

When control is running from the USING block to end or throw an exception, it is performed. Remember that your illustrated object must implement the System.IDisposable interface. The using statement defines a range that will be cleared. Despite this, we spend some time to implement the IDisposable interface, what if the customer does not call them? For this purpose C # has a cool solution. 'Using' code block. It looks like this: Conclusion When a customer calls idisposable.dispose (), the customer specially wants to clean up the managed and unmanaged resources, and therefore complete the cleanup work. One thing you have to pay attention is to call GC.SupressFinalize (this) immediately after cleaning the resource. This method notifies the garbage collection device that does not need to call the decchor because we have made it clean. Note The above example, the decchor uses the parameter false to call Dispose. Here, we are confident that the garbage collection device collects managed resources. We only do the cleanup of unmanaged resources. Public class myclass: idisposable

{

Private bool isdisposed = false;

Public void dispose ()

{

Dispose (TRUE);

Gc.supressFinalize (this);

}

Protected Void Dispose (BOOL DIPOSIING)

{

IF (! isdisposed)

{

IF (Disposing)

{

// clean up management resources}

// Clean Up Unmanaged Resources

}

Isdisposed = true;

}

~ Myclass ()

{

Dispose (false);

}

} Joint Utilizer and IDisposable interface

Call the garbage collection device to clean. Implement IDisposable DISPOSE method. Call the garbage collection device You can force the garbage collection device by calling the gc.collect method to clean the memory, but in most cases, this should avoid because it will result in performance issues. In the above program, the comments are removed at GC.COLLECT (). Compile and run it. Now, you can see that the decchor is executed in the console. The programmer cannot control when the decchor will be executed because it is determined by the garbage collector. The garbage collector checks the objects that are not used by the application. It believes that these conditions are clear and retract their memory. The decchor is also called when the program exits. The scene that occurs behind the decchor is implicitly called the object base class when the decons is performed. Therefore, the above-described decchor code is implied to:

转载请注明原文地址:https://www.9cbs.com/read-129571.html

New Post(0)