what is destructor | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 5

what is destructor

thanks

6th Sep 2016, 11:51 AM
Salad
Salad - avatar
6 Answers
+ 7
In the same way as a constructor executes a code block when an object is instantiated, destructors are simply blocks of code that execute when an object is uninstantiated. Typically destructors are used to ensure that resources are freed up when the object no longer has a purpose, but there is no reason these cannot trigger further events within your application.
6th Sep 2016, 2:33 PM
Laurence Turner
Laurence Turner - avatar
+ 5
In general, C# does not require as much memory management as is needed when you develop with a language that does not target a runtime with garbage collection.
6th Sep 2016, 11:52 AM
Salad
Salad - avatar
+ 4
Objects are dynamically allocated from a pool of free memory by using the new operator. Of course, memory is not infinite, and the free memory can be exhausted. Thus, it is possible for new to fail because there is insufficient free memory to create the desired object. for this reason, one of the key components of any dynamic allocation scheme is the recovery of free memory from unused objects, making that memory available for subsequent reallocation. In many programming languages, the release of previously allocated memory is handled manually. for example, in C++, the delete operator is used to free memory that was allocated. However, C# uses a different, more trouble-free approach: garbage collection. C#'s garbage collection system reclaims objects automatically-occuring transparently occypied by the object eventuaaly released and collected. This recycled memory can then be used for a subsequent allocation. Destructors in C#: Destructors in C# language can be used in some highly specialized situaltions to ensure that an object terminates cleanly. For example, you might use a destructor to ensure that a system resource owned by an object is released. It must be stated at the outset that destructors are a very advanced feature that are applicable only to certain rare cases. They are not normally needed. They are briefly described here for completeness. ~class-name() { //destruction code }
10th Dec 2016, 7:15 PM
Paras Jain
Paras Jain - avatar
+ 2
A constructor initializes an object when it is created. it has the same name as its class and is syntactically similar to a method. However, constructors have no explicit return type. The general form of a constructor is shown here: access class-name(param-list) { //Constructor code }
10th Dec 2016, 6:51 PM
Paras Jain
Paras Jain - avatar
+ 2
Yes Seenu :)
11th Jun 2017, 11:03 AM
Salad
Salad - avatar
0
You know about constructor and when constructor will work to start??
21st Nov 2016, 2:59 PM
seenu Manikandan
seenu Manikandan - avatar