Tutorials to .com

Tutorials to .com » Dotnet » Csharp » C # Sharp Experience (5.3)

C # Sharp Experience (5.3)

Print View , by: iSee ,Total views: 23 ,Word Count: 7729 ,Date: Tue, 25 Aug 2009 Time: 10:07 PM

Destructor since. NET platform, automatic garbage collection mechanism, c # language in the class destructor is no longer such as traditional C++ + + if necessary, take an object destructor is no longer a member of the memory release - automatic garbage collection mechanism to ensure the recovery of memory . In fact C # has no delete operation! Destructor is responsible only for recovery of resources to deal with those non-system, a typical example: open file, get the window handles, database connections, network connections, and so require the user to release it-yourself non-memory resources. We look at the output of the following examples: using System; class MyClass1 (~ MyClass1 () (Console.WriteLine ( "MyClass1's destructor");)) class MyClass2: MyClass1 (~ MyClass2 () (Console.WriteLine ( "MyClass2's destructor"); )) public class Test (public static void Main () (MyClass2 MyObject = new MyClass2 (); MyObject = null; GC.Collect (); GC.WaitForPendingFinalizers ();)) Compile and run the program can get the following output: MyClass2's destructor MyClass1's destructor in which the program is to ensure that the last two class destructor to be called. GC.Collect () is forced to start common language runtime garbage collection thread recycling. The GC.WaitForPendingFinalizers () is pending the termination of the current thread to wait for the whole of (Finalizaion) operation completion. Termination of (Finalizaion) operations to ensure the class destructor is executed, it will explain in detail in the following. Destructors are not inherited, that category must be a clear statement destructor, the class was there destructors. Users to achieve destructor, the compiler automatically add the call to the parent class destructor, which Finalize method, the following will explain in detail. Destructor because garbage collection mechanism will be at the right time to automatically call the user are neither called destructors. Only instance destructor, but not a static destructors. So how the destructor is automatically called? This. Net garbage collection mechanism by a known termination of (Finalizaion) to support the operation. . Net-based operating system, the termination of the default without any action, if the user need to release unmanaged resources, the user as long as the destructor can be achieved within such an operation - this is C # recommended practices. We see the following code: using System; class MyClass1 (~ MyClass1 () (Console.WritleLine ( "MyClass1 Destructor");)) In fact, from the middle of the generated code to see us can see that the code is converted into the following code: using System; class MyClass1 (protected override void Finalize () (try (Console.WritleLine ( "My Class1 Destructor");) finally (base.Finalize ();))) In fact C # compiler does not allow user's own overloaded or call Finalize method - the compiler completely shield the parent class Finalize method (C #, due to the nature of single inheritance, System.Object class is the ancestor class of all classes, naturally each class has a Finalize method), Such an approach does not seem to exist. We see the following code is actually wrong: using System; class MyClass (override protected void Finalize () () / / error public void MyMethod () (this.Finalize ();// error)) but the following code is is correct: using System; class MyClass (public void Finalize () (Console.WriteLine ( "My Class Destructor");)) public class Test (public static void Main () (MyClass MyObject = new MyClass (); MyObject. Finalize ();)) In fact Here Finalize method has been completely detached from the "termination of operations" and semantics, and to become C # language, a general alternative. It is worth noting that it is also shielded the parent class's Finalize method of System.Object, so be careful! Termination of operation is. Net run time, there are many restrictions, are often not recommended to achieve. When an object implements the terminator (Finalizer), the run-time will be a reference to this object by adding a reference set of objects known as the termination of the queue, as called for an end of a sign. When garbage collection starts, if an object is no longer referenced, but it was added the termination of the queue object reference set, then the run-time objects are not immediately available for garbage collection, but marked to require the termination of this object-oriented operation of the object. When garbage collection is completed, the termination of the thread will be run-time wake-up the implementation of the termination of operations. Obviously after this set of object references from the end of the list will be deleted. But only until the next garbage collection, this object actually start to garbage collection, the object's memory resources was only a real recovery. Easy to see out of the termination of operation so that garbage collection had two, which will bring no small system overhead. The termination of the thread through the opening of mechanisms to achieve, which has a thread-safety issue. . Net runtime can not guarantee that the implementation of the termination of the order, meaning that if the object A has a reference pointing to an object B, the two objects have the termination of operation, but the object A in the termination of operation is not necessarily a valid object A reference. . Net run-time does not allow users to run the program directly call Finalize () method. If the user pressing need for such operations, can implement IDisposable interface to provide a public Dispose () method. Need to explain is to provide a Dispose () method, is still the need to provide Finalize method of operation, namely, the achievement on the pretext of destructor. Because the Dispose () method and does not guarantee is called. So. Net run-time is not recommended the termination of the object-oriented operation that provides a destructor, but in a non-managed resources, such as a database connection, the file open, and so the need for strict released only when required to do so. Most of the time, refuse collection should be submitted. Net runtime to control, but sometimes may need to artificially control the look at garbage collection operation. For example, in operating a large-scale collection of objects, we are convinced that these objects are no longer conduct any operations, and then we can force garbage collection to implement immediately, which by calling System.GC.Collect () method can be realized, But the frequent collection would be significantly reduced system performance. In another case, have an object into an object reference to the termination of the chain on the set, but if we are in the program has already been done in some parts of the termination of operation, namely a clear call to Dispose () method, after which can by calling System.GC.SupressFinalize () to terminate the object reference object references from a set of chain take off, to ignore the termination of operations. Termination of operation of the system burden is heavy. In-depth understanding of. NET runtime automatically garbage collection feature, we will understand the C # destructor Why such a big turn around to realize our programming needs, in order to put the memory resources and the recovery of non-memory resources its capability to do - this is precisely the destructor of the primitive! Destructor since. NET platform, automatic garbage collection mechanism, C # language in the class destructor is no longer such as traditional C + + if necessary, take an object destructor is no longer a member of the memory release - automatic garbage collection mechanism to ensure the recovery of memory . In fact C # has no delete operation! Destructor is responsible only for recovery of resources to deal with those non-system, a typical example: open file, get the window handles, database connections, network connections, and so require the user to release it-yourself non-memory resources. We look at the output of the following examples: using System; class MyClass1 (~ MyClass1 () (Console.WriteLine ( "MyClass1's destructor");)) class MyClass2: MyClass1 (~ MyClass2 () (Console.WriteLine ( "MyClass2's destructor"); )) public class Test (public static void Main () (MyClass2 MyObject = new MyClass2 (); MyObject = null; GC.Collect (); GC.WaitForPendingFinalizers ();)) Compile and run the program can get the following output: MyClass2's destructor MyClass1's destructor in which the program is to ensure that the last two class destructor to be called. GC.Collect () is forced to start common language runtime garbage collection thread recycling. The GC.WaitForPendingFinalizers () is pending the termination of the current thread to wait for the whole of (Finalizaion) operation completion. Termination of (Finalizaion) operations to ensure the class destructor is executed, it will explain in detail in the following. Destructors are not inherited, that category must be a clear statement destructor, the class was there destructors. Users to achieve destructor, the compiler automatically add the call to the parent class destructor, which Finalize method, the following will explain in detail. Destructor because garbage collection mechanism will be at the right time to automatically call the user are neither called destructors. Only instance destructor, but not a static destructors. So how the destructor is automatically called? This. Net garbage collection mechanism by a known termination of (Finalizaion) to support the operation. . Net-based operating system, the termination of the default without any action, if the user need to release unmanaged resources, the user as long as the destructor can be achieved within such an operation - this is C # recommended practices. We see the following code: using System; class MyClass1 (~ MyClass1 () (Console.WritleLine ( "MyClass1 Destructor");)) In fact, from the middle of the generated code to see us can see that the code is converted into the following code: using System; class MyClass1 (protected override void Finalize () (try (Console.WritleLine ( "My Class1 Destructor");) finally (base.Finalize ();))) In fact C # compiler does not allow user's own overloaded or call Finalize method - the compiler completely shield the parent class Finalize method (C #, due to the nature of single inheritance, System.Object class is the ancestor class of all classes, naturally each class has a Finalize method), Such an approach does not seem to exist. We see the following code is actually wrong: using System; class MyClass (override protected void Finalize () () / / error public void MyMethod () (this.Finalize ();// error)) but the following code is is correct: using System; class MyClass (public void Finalize () (Console.WriteLine ( "My Class Destructor");)) public class Test (public static void Main () (MyClass MyObject = new MyClass (); MyObject. Finalize ();)) In fact Here Finalize method has been completely detached from the "termination of operations" and semantics, and to become C # language, a general alternative. It is worth noting that it is also shielded the parent class's Finalize method of System.Object, so be careful! Termination of operation is. Net run time, there are many restrictions, are often not recommended to achieve. When an object implements the terminator (Finalizer), the run-time will be a reference to this object by adding a reference set of objects known as the termination of the queue, as called for an end of a sign. When garbage collection starts, if an object is no longer referenced, but it was added the termination of the queue object reference set, then the run-time objects are not immediately available for garbage collection, but marked to require the termination of this object-oriented operation of the object. When garbage collection is completed, the termination of the thread will be run-time wake-up the implementation of the termination of operations. Obviously after this set of object references from the end of the list will be deleted. But only until the next garbage collection, this object actually start to garbage collection, the object's memory resources was only a real recovery. Easy to see out of the termination of operation so that garbage collection had two, which will bring no small system overhead. The termination of the thread through the opening of mechanisms to achieve, which has a thread-safety issue. . Net runtime can not guarantee that the implementation of the termination of the order, meaning that if the object A has a reference pointing to an object B, the two objects have the termination of operation, but the object A in the termination of operation is not necessarily a valid object A reference. . Net run-time does not allow users to run the program directly call Finalize () method. If the user pressing need for such operations, can implement IDisposable interface to provide a public Dispose () method. Need to explain is to provide a Dispose () method, is still the need to provide Finalize method of operation, namely, the achievement on the pretext of destructor. Because the Dispose () method and does not guarantee is called. So. Net run-time is not recommended the termination of the object-oriented operation that provides a destructor, but in a non-managed resources, such as a database connection, the file open, and so the need for strict released only when required to do so. Most of the time, refuse collection should be submitted. Net runtime to control, but sometimes may need to artificially control the look at garbage collection operation. For example, in operating a large-scale collection of objects, we are convinced that these objects are no longer conduct any operations, and then we can force garbage collection to implement immediately, which by calling System.GC.Collect () method can be realized, But the frequent collection would be significantly reduced system performance. In another case, have an object into an object reference to the termination of the chain on the set, but if we are in the program has already been done in some parts of the termination of operation, namely a clear call to Dispose () method, after which can by calling System.GC.SupressFinalize () to terminate the object reference object references from a set of chain take off, to ignore the termination of operations. Termination of operation of the system burden is heavy. In-depth understanding of. NET runtime automatically garbage collection feature, we will understand the C # destructor Why such a big turn around to realize our programming needs, in order to put the memory resources and the recovery of non-memory resources its capability to do - this is precisely the destructor of the primitive! Destructor since. NET platform, automatic garbage collection mechanism, C # language in the class destructor is no longer such as traditional C + + if necessary, take an object destructor is no longer a member of the memory release - automatic garbage collection mechanism to ensure the recovery of memory . In fact C # has no delete operation! Destructor is responsible only for recovery of resources to deal with those non-system, a typical example: open file, get the window handles, database connections, network connections, and so require the user to release it-yourself non-memory resources. We look at the output of the following examples: using System; class MyClass1 (~ MyClass1 () (Console.WriteLine ( "MyClass1's destructor");)) class MyClass2: MyClass1 (~ MyClass2 () (Console.WriteLine ( "MyClass2's destructor"); )) public class Test (public static void Main () (MyClass2 MyObject = new MyClass2 (); MyObject = null; GC.Collect (); GC.WaitForPendingFinalizers ();)) Compile and run the program can get the following output: MyClass2's destructor MyClass1's destructor in which the program is to ensure that the last two class destructor to be called. GC.Collect () is forced to start common language runtime garbage collection thread recycling. The GC.WaitForPendingFinalizers () is pending the termination of the current thread to wait for the whole of (Finalizaion) operation completion. Termination of (Finalizaion) operations to ensure the class destructor is executed, it will explain in detail in the following. Destructors are not inherited, that category must be a clear statement destructor, the class was there destructors. Users to achieve destructor, the compiler automatically add the call to the parent class destructor, which Finalize method, the following will explain in detail. Destructor because garbage collection mechanism will be at the right time to automatically call the user are neither called destructors. Only instance destructor, but not a static destructors. So how the destructor is automatically called? This. Net garbage collection mechanism by a known termination of (Finalizaion) to support the operation. . Net-based operating system, the termination of the default without any action, if the user need to release unmanaged resources, the user as long as the destructor can be achieved within such an operation - this is C # recommended practices. We see the following code: using System; class MyClass1 (~ MyClass1 () (Console.WritleLine ( "MyClass1 Destructor");)) In fact, from the middle of the generated code to see us can see that the code is converted into the following code: using System; class MyClass1 (protected override void Finalize () (try (Console.WritleLine ( "My Class1 Destructor");) finally (base.Finalize ();))) In fact C # compiler does not allow user's own overloaded or call Finalize method - the compiler completely shield the parent class Finalize method (C #, due to the nature of single inheritance, System.Object class is the ancestor class of all classes, naturally each class has a Finalize method), Such an approach does not seem to exist. We see the following code is actually wrong: using System; class MyClass (override protected void Finalize () () / / error public void MyMethod () (this.Finalize ();// error)) but the following code is is correct: using System; class MyClass (public void Finalize () (Console.WriteLine ( "My Class Destructor");)) public class Test (public static void Main () (MyClass MyObject = new MyClass (); MyObject. Finalize ();)) In fact Here Finalize method has been completely detached from the "termination of operations" and semantics, and to become C # language, a general alternative. It is worth noting that it is also shielded the parent class's Finalize method of System.Object, so be careful! Termination of operation is. Net run time, there are many restrictions, are often not recommended to achieve. When an object implements the terminator (Finalizer), the run-time will be a reference to this object by adding a reference set of objects known as the termination of the queue, as called for an end of a sign. When garbage collection starts, if an object is no longer referenced, but it was added the termination of the queue object reference set, then the run-time objects are not immediately available for garbage collection, but marked to require the termination of this object-oriented operation of the object. When garbage collection is completed, the termination of the thread will be run-time wake-up the implementation of the termination of operations. Obviously after this set of object references from the end of the list will be deleted. But only until the next garbage collection, this object actually start to garbage collection, the object's memory resources was only a real recovery. Easy to see out of the termination of operation so that garbage collection had two, which will bring no small system overhead. The termination of the thread through the opening of mechanisms to achieve, which has a thread-safety issue. . Net runtime can not guarantee that the implementation of the termination of the order, meaning that if the object A has a reference pointing to an object B, the two objects have the termination of operation, but the object A in the termination of operation is not necessarily a valid object A reference. . Net run-time does not allow users to run the program directly call Finalize () method. If the user pressing need for such operations, can implement IDisposable interface to provide a public Dispose () method. Need to explain is to provide a Dispose () method, is still the need to provide Finalize method of operation, namely, the achievement on the pretext of destructor. Because the Dispose () method and does not guarantee is called. So. Net run-time is not recommended the termination of the object-oriented operation that provides a destructor, but in a non-managed resources, such as a database connection, the file open, and so the need for strict released only when required to do so. Most of the time, refuse collection should be submitted. Net runtime to control, but sometimes may need to artificially control the look at garbage collection operation. For example, in operating a large-scale collection of objects, we are convinced that these objects are no longer conduct any operations, and then we can force garbage collection to implement immediately, which by calling System.GC.Collect () method can be realized, But the frequent collection would be significantly reduced system performance. In another case, have an object into an object reference to the termination of the chain on the set, but if we are in the program has already been done in some parts of the termination of operation, namely a clear call to Dispose () method, after which can by calling System.GC.SupressFinalize () to terminate the object reference object references from a set of chain take off, to ignore the termination of operations. Termination of operation of the system burden is heavy. In-depth understanding of. NET runtime automatically garbage collection feature, we will understand the C # destructor Why such a big turn around to realize our programming needs, in order to put the memory resources and the recovery of non-memory resources its capability to do - this is precisely the destructor of the primitive! Destructor since. NET platform, automatic garbage collection mechanism, C # language in the class destructor is no longer such as traditional C + + if necessary, take an object destructor is no longer a member of the memory release - automatic garbage collection mechanism to ensure the recovery of memory . In fact C # has no delete operation! Destructor is responsible only for recovery of resources to deal with those non-system, a typical example: open file, get the window handles, database connections, network connections, and so require the user to release it-yourself non-memory resources. We look at the output of the following examples: using System; class MyClass1 (~ MyClass1 () (Console.WriteLine ( "MyClass1's destructor");)) class MyClass2: MyClass1 (~ MyClass2 () (Console.WriteLine ( "MyClass2's destructor"); )) public class Test (public static void Main () (MyClass2 MyObject = new MyClass2 (); MyObject = null; GC.Collect (); GC.WaitForPendingFinalizers ();)) Compile and run the program can get the following output: MyClass2's destructor MyClass1's destructor in which the program is to ensure that the last two class destructor to be called. GC.Collect () is forced to start common language runtime garbage collection thread recycling. The GC.WaitForPendingFinalizers () is pending the termination of the current thread to wait for the whole of (Finalizaion) operation completion. Termination of (Finalizaion) operations to ensure the class destructor is executed, it will explain in detail in the following. Destructors are not inherited, that category must be a clear statement destructor, the class was there destructors. Users to achieve destructor, the compiler automatically add the call to the parent class destructor, which Finalize method, the following will explain in detail. Destructor because garbage collection mechanism will be at the right time to automatically call the user are neither called destructors. Only instance destructor, but not a static destructors. So how the destructor is automatically called? This. Net garbage collection mechanism by a known termination of (Finalizaion) to support the operation. . Net-based operating system, the termination of the default without any action, if the user need to release unmanaged resources, the user as long as the destructor can be achieved within such an operation - this is C # recommended practices. We see the following code: using System; class MyClass1 (~ MyClass1 () (Console.WritleLine ( "MyClass1 Destructor");)) In fact, from the middle of the generated code to see us can see that the code is converted into the following code: using System; class MyClass1 (protected override void Finalize () (try (Console.WritleLine ( "My Class1 Destructor");) finally (base.Finalize ();))) In fact C # compiler does not allow user's own overloaded or call Finalize method - the compiler completely shield the parent class Finalize method (C #, due to the nature of single inheritance, System.Object class is the ancestor class of all classes, naturally each class has a Finalize method), Such an approach does not seem to exist. We see the following code is actually wrong: using System; class MyClass (override protected void Finalize () () / / error public void MyMethod () (this.Finalize ();// error)) but the following code is is correct: using System; class MyClass (public void Finalize () (Console.WriteLine ( "My Class Destructor");)) public class Test (public static void Main () (MyClass MyObject = new MyClass (); MyObject. Finalize ();)) In fact Here Finalize method has been completely detached from the "termination of operations" and semantics, and to become C # language, a general alternative. It is worth noting that it is also shielded the parent class's Finalize method of System.Object, so be careful! Termination of operation is. Net run time, there are many restrictions, are often not recommended to achieve. When an object implements the terminator (Finalizer), the run-time will be a reference to this object by adding a reference set of objects known as the termination of the queue, as called for an end of a sign. When garbage collection starts, if an object is no longer referenced, but it was added the termination of the queue object reference set, then the run-time objects are not immediately available for garbage collection, but marked to require the termination of this object-oriented operation of the object. When garbage collection is completed, the termination of the thread will be run-time wake-up the implementation of the termination of operations. Obviously after this set of object references from the end of the list will be deleted. But only until the next garbage collection, this object actually start to garbage collection, the object's memory resources was only a real recovery. Easy to see out of the termination of operation so that garbage collection had two, which will bring no small system overhead. The termination of the thread through the opening of mechanisms to achieve, which has a thread-safety issue. . Net runtime can not guarantee that the implementation of the termination of the order, meaning that if the object A has a reference pointing to an object B, the two objects have the termination of operation, but the object A in the termination of operation is not necessarily a valid object A reference. . Net run-time does not allow users to run the program directly call Finalize () method. If the user pressing need for such operations, can implement IDisposable interface to provide a public Dispose () method. Need to explain is to provide a Dispose () method, is still the need to provide Finalize method of operation, namely, the achievement on the pretext of destructor. Because the Dispose () method and does not guarantee is called. So. Net run-time is not recommended the termination of the object-oriented operation that provides a destructor, but in a non-managed resources, such as a database connection, the file open, and so the need for strict released only when required to do so. Most of the time, refuse collection should be submitted. Net runtime to control, but sometimes may need to artificially control the look at garbage collection operation. For example, in operating a large-scale collection of objects, we are convinced that these objects are no longer conduct any operations, and then we can force garbage collection to implement immediately, which by calling System.GC.Collect () method can be realized, But the frequent collection would be significantly reduced system performance. In another case, have an object into an object reference to the termination of the chain on the set, but if we are in the program has already been done in some parts of the termination of operation, namely a clear call to Dispose () method, after which can by calling System.GC.SupressFinalize () to terminate the object reference object references from a set of chain take off, to ignore the termination of operations. Termination of operation of the system burden is heavy. In-depth understanding of. NET runtime automatically garbage collection feature, we will understand the C # destructor Why such a big turn around to realize our programming needs, in order to put the memory resources and the recovery of non-memory resources its capability to do - this is precisely the destructor of the primitive! Destructor since. NET platform, automatic garbage collection mechanism, C # language in the class destructor is no longer such as traditional C + + if necessary, take an object destructor is no longer a member of the memory release - automatic garbage collection mechanism to ensure the recovery of memory . In fact C # has no delete operation! Destructor is responsible only for recovery of resources to deal with those non-system, a typical example: open file, get the window handles, database connections, network connections, and so require the user to release it-yourself non-memory resources. We look at the output of the following examples: using System; class MyClass1 (~ MyClass1 () (Console.WriteLine ( "MyClass1's destructor");)) class MyClass2: MyClass1 (~ MyClass2 () (Console.WriteLine ( "MyClass2's destructor"); )) public class Test (public static void Main () (MyClass2 MyObject = new MyClass2 (); MyObject = null; GC.Collect (); GC.WaitForPendingFinalizers ();)) Compile and run the program can get the following output: MyClass2's destructor MyClass1's destructor in which the program is to ensure that the last two class destructor to be called. GC.Collect () is forced to start common language runtime garbage collection thread recycling. The GC.WaitForPendingFinalizers () is pending the termination of the current thread to wait for the whole of (Finalizaion) operation completion. Termination of (Finalizaion) operations to ensure the class destructor is executed, it will explain in detail in the following. Destructors are not inherited, that category must be a clear statement destructor, the class was there destructors. Users to achieve destructor, the compiler automatically add the call to the parent class destructor, which Finalize method, the following will explain in detail. Destructor because garbage collection mechanism will be at the right time to automatically call the user are neither called destructors. Only instance destructor, but not a static destructors. So how the destructor is automatically called? This. Net garbage collection mechanism by a known termination of (Finalizaion) to support the operation. . Net-based operating system, the termination of the default without any action, if the user need to release unmanaged resources, the user as long as the destructor can be achieved within such an operation - this is C # recommended practices. We see the following code: using System; class MyClass1 (~ MyClass1 () (Console.WritleLine ( "MyClass1 Destructor");)) In fact, from the middle of the generated code to see us can see that the code is converted into the following code: using System; class MyClass1 (protected override void Finalize () (try (Console.WritleLine ( "My Class1 Destructor");) finally (base.Finalize ();))) In fact C # compiler does not allow user's own overloaded or call Finalize method - the compiler completely shield the parent class Finalize method (C #, due to the nature of single inheritance, System.Object class is the ancestor class of all classes, naturally each class has a Finalize method), Such an approach does not seem to exist. We see the following code is actually wrong: using System; class MyClass (override protected void Finalize () () / / error public void MyMethod () (this.Finalize ();// error)) but the following code is is correct: using System; class MyClass (public void Finalize () (Console.WriteLine ( "My Class Destructor");)) public class Test (public static void Main () (MyClass MyObject = new MyClass (); MyObject. Finalize ();)) In fact Here Finalize method has been completely detached from the "termination of operations" and semantics, and to become C # language, a general alternative. It is worth noting that it is also shielded the parent class's Finalize method of System.Object, so be careful! Termination of operation is. Net run time, there are many restrictions, are often not recommended to achieve. When an object implements the terminator (Finalizer), the run-time will be a reference to this object by adding a reference set of objects known as the termination of the queue, as called for an end of a sign. When garbage collection starts, if an object is no longer referenced, but it was added the termination of the queue object reference set, then the run-time objects are not immediately available for garbage collection, but marked to require the termination of this object-oriented operation of the object. When garbage collection is completed, the termination of the thread will be run-time wake-up the implementation of the termination of operations. Obviously after this set of object references from the end of the list will be deleted. But only until the next garbage collection, this object actually start to garbage collection, the object's memory resources was only a real recovery. Easy to see out of the termination of operation so that garbage collection had two, which will bring no small system overhead. The termination of the thread through the opening of mechanisms to achieve, which has a thread-safety issue. . Net runtime can not guarantee that the implementation of the termination of the order, meaning that if the object A has a reference pointing to an object B, the two objects have the termination of operation, but the object A in the termination of operation is not necessarily a valid object A reference. . Net run-time does not allow users to run the program directly call Finalize () method. If the user pressing need for such operations, can implement IDisposable interface to provide a public Dispose () method. Need to explain is to provide a Dispose () method, is still the need to provide Finalize method of operation, namely, the achievement on the pretext of destructor. Because the Dispose () method and does not guarantee is called. So. Net run-time is not recommended the termination of the object-oriented operation that provides a destructor, but in a non-managed resources, such as a database connection, the file open, and so the need for strict released only when required to do so. Most of the time, refuse collection should be submitted. Net runtime to control, but sometimes may need to artificially control the look at garbage collection operation. For example, in operating a large-scale collection of objects, we are convinced that these objects are no longer conduct any operations, and then we can force garbage collection to implement immediately, which by calling System.GC.Collect () method can be realized, But the frequent collection would be significantly reduced system performance. In another case, have an object into an object reference to the termination of the chain on the set, but if we are in the program has already been done in some parts of the termination of operation, namely a clear call to Dispose () method, after which can by calling System.GC.SupressFinalize () to terminate the object reference object references from a set of chain take off, to ignore the termination of operations. Termination of operation of the system burden is heavy. In-depth understanding of. NET runtime automatically garbage collection feature, we will understand the C # destructor Why such a big turn around to realize our programming needs, in order to put the memory resources and the recovery of non-memory resources its capability to do - this is precisely the destructor of the primitive! Destructor since. NET platform, automatic garbage collection mechanism, C # language in the class destructor is no longer such as traditional C + + if necessary, take an object destructor is no longer a member of the memory release - automatic garbage collection mechanism to ensure the recovery of memory . In fact C # has no delete operation!,:,,,。: using System; class MyClass1 {     ~MyClass1()     {         Console.WriteLine("MyClass1's destructor");     } } class MyClass2: MyClass1 {     ~MyClass2()     {         Console.WriteLine("MyClass2's destructor");     } } public class Test {     public static void Main()     {         MyClass2 MyObject = new MyClass2();         MyObject = null;         GC.Collect();         GC.WaitForPendingFinalizers();     } }   : MyClass2's destructor MyClass1's destructor   。 GC.Collect()。GC.WaitForPendingFinalizers()(Finalizaion)。(Finalizaion),。   ,,。,,Finalize。,。,。   ?.Net(Finalizaion)。 .Net,,--C#。: using System; class MyClass1 {     ~MyClass1()     {         Console.WritleLine("MyClass1 Destructor");     } }   ,,: using System; class MyClass1 {     protected override void Finalize()     {         try         { Console.WritleLine("My Class1 Destructor");         }         finally         {             base.Finalize();         }     } }   C#Finalize--Finalize(C#,System.Object,Finalize),。: using System; class MyClass {     override protected void Finalize() {}// public void MyMethod()     {         this.Finalize();// } }   : using System; class MyClass {     public void Finalize()     {         Console.WriteLine("My Class Destructor");     } } public class Test {     public static void Main()     {         MyClass MyObject=new MyClass();         MyObject.Finalize();     } }   Finalize“”,C#。System.ObjectFinalize,!   .Net,。(Finalizer),,。,,,。,。。,,。,,。,。 .Net,AB,,AA。 .NetFinalize()。,IDisposableDispose()。Dispose(),Finalize,。Dispose()。.Net,,,。   ,.Net,,。,,,System.GC.Collect() ,。,,,Dispose(),System.GC.SupressFinalize(),。。   .NET,C#,--!     .NET,C#C++,--。C#delete!,:,,,。: using System; class MyClass1 {     ~MyClass1()     {         Console.WriteLine("MyClass1's destructor");     } } class MyClass2: MyClass1 {     ~MyClass2()     {         Console.WriteLine("MyClass2's destructor");     } } public class Test {     public static void Main()     {         MyClass2 MyObject = new MyClass2();         MyObject = null;         GC.Collect();         GC.WaitForPendingFinalizers();     } }   : MyClass2's destructor MyClass1's destructor   。 GC.Collect()。GC.WaitForPendingFinalizers()(Finalizaion)。(Finalizaion),。   ,,。,,Finalize。,。,。   ?.Net(Finalizaion)。 .Net,,--C#。: using System; class MyClass1 {     ~MyClass1()     {         Console.WritleLine("MyClass1 Destructor");     } }   ,,: using System; class MyClass1 {     protected override void Finalize()     {         try         { Console.WritleLine("My Class1 Destructor");         }         finally         {             base.Finalize();         }     } }   C#Finalize--Finalize(C#,System.Object,Finalize),。: using System; class MyClass {     override protected void Finalize() {}// public void MyMethod()     {         this.Finalize();// } }   : using System; class MyClass {     public void Finalize()     {         Console.WriteLine("My Class Destructor");     } } public class Test {     public static void Main()     {         MyClass MyObject=new MyClass();         MyObject.Finalize();     } }   Finalize“”,C#。System.ObjectFinalize,!   .Net,。(Finalizer),,。,,,。,。。,,。,,。,。 .Net,AB,,AA。 .NetFinalize()。,IDisposableDispose()。Dispose(),Finalize,。Dispose()。.Net,,,。   ,.Net,,。,,,System.GC.Collect() ,。,,,Dispose(),System.GC.SupressFinalize(),。。   .NET,C#,--!     .NET,C#C++,--。C#delete!,:,,,。: using System; class MyClass1 {     ~MyClass1()     {         Console.WriteLine("MyClass1's destructor");     } } class MyClass2: MyClass1 {     ~MyClass2()     {         Console.WriteLine("MyClass2's destructor");     } } public class Test {     public static void Main()     {         MyClass2 MyObject = new MyClass2();         MyObject = null;         GC.Collect();         GC.WaitForPendingFinalizers();     } }   : MyClass2's destructor MyClass1's destructor   。 GC.Collect()。GC.WaitForPendingFinalizers()(Finalizaion)。(Finalizaion),。   ,,。,,Finalize。,。,。   ?.Net(Finalizaion)。 .Net,,--C#。: using System; class MyClass1 {     ~MyClass1()     {         Console.WritleLine("MyClass1 Destructor");     } }   ,,: using System; class MyClass1 {     protected override void Finalize()     {         try         { Console.WritleLine("My Class1 Destructor");         }         finally         {             base.Finalize();         }     } }   C#Finalize--Finalize(C#,System.Object,Finalize),。: using System; class MyClass {     override protected void Finalize() {}// public void MyMethod()     {         this.Finalize();// } }   : using System; class MyClass {     public void Finalize()     {         Console.WriteLine("My Class Destructor");     } } public class Test {     public static void Main()     {         MyClass MyObject=new MyClass();         MyObject.Finalize();     } }   Finalize“”,C#。System.ObjectFinalize,!   .Net,。(Finalizer),,。,,,。,。。,,。,,。,。 .Net,AB,,AA。 .NetFinalize()。,IDisposableDispose()。Dispose(),Finalize,。Dispose()。.Net,,,。   ,.Net,,。,,,System.GC.Collect() ,。,,,Dispose(),System.GC.SupressFinalize(),。。   .NET,C#,--!     .NET,C#C++,--。C#delete!,:,,,。: using System; class MyClass1 {     ~MyClass1()     {         Console.WriteLine("MyClass1's destructor");     } } class MyClass2: MyClass1 {     ~MyClass2()     {         Console.WriteLine("MyClass2's destructor");     } } public class Test {     public static void Main()     {         MyClass2 MyObject = new MyClass2();         MyObject = null;         GC.Collect();         GC.WaitForPendingFinalizers();     } }   : MyClass2's destructor MyClass1's destructor   。 GC.Collect()。GC.WaitForPendingFinalizers()(Finalizaion)。(Finalizaion),。   ,,。,,Finalize。,。,。   ?.Net(Finalizaion)。 .Net,,--C#。: using System; class MyClass1 {     ~MyClass1()     {         Console.WritleLine("MyClass1 Destructor");     } }   ,,: using System; class MyClass1 {     protected override void Finalize()     {         try         { Console.WritleLine("My Class1 Destructor");         }         finally         {             base.Finalize();         }     } }   C#Finalize--Finalize(C#,System.Object,Finalize),。: using System; class MyClass {     override protected void Finalize() {}// public void MyMethod()     {         this.Finalize();// } }   : using System; class MyClass {     public void Finalize()     {         Console.WriteLine("My Class Destructor");     } } public class Test {     public static void Main()     {         MyClass MyObject=new MyClass();         MyObject.Finalize();     } }   Finalize“”,C#。System.ObjectFinalize,!   .Net,。(Finalizer),,。,,,。,。。,,。,,。,。 .Net,AB,,AA。 .NetFinalize()。,IDisposableDispose()。Dispose(),Finalize,。Dispose()。.Net,,,。   ,.Net,,。,,,System.GC.Collect() ,。,,,Dispose(),System.GC.SupressFinalize(),。。   .NET,C#,--!     .NET,C#C++,--。C#delete!,:,,,。: using System; class MyClass1 {     ~MyClass1()     {         Console.WriteLine("MyClass1's destructor");     } } class MyClass2: MyClass1 {     ~MyClass2()     {         Console.WriteLine("MyClass2's destructor");     } } public class Test {     public static void Main()     {         MyClass2 MyObject = new MyClass2();         MyObject = null;         GC.Collect();         GC.WaitForPendingFinalizers();     } }   : MyClass2's destructor MyClass1's destructor   。 GC.Collect()。GC.WaitForPendingFinalizers()(Finalizaion)。(Finalizaion),。   ,,。,,Finalize。,。,。   ?.Net(Finalizaion)。 .Net,,--C#。: using System; class MyClass1 {     ~MyClass1()     {         Console.WritleLine("MyClass1 Destructor");     } }   ,,: using System; class MyClass1 {     protected override void Finalize()     {         try         { Console.WritleLine("My Class1 Destructor");         }         finally         {             base.Finalize();         }     } }   C#Finalize--Finalize(C#,System.Object,Finalize),。: using System; class MyClass {     override protected void Finalize() {}// public void MyMethod()     {         this.Finalize();// } }   : using System; class MyClass {     public void Finalize()     {         Console.WriteLine("My Class Destructor");     } } public class Test {     public static void Main()     {         MyClass MyObject=new MyClass();         MyObject.Finalize();     } }   Finalize“”,C#。System.ObjectFinalize,!   .Net,。(Finalizer),,。,,,。,。。,,。,,。,。 .Net,AB,,AA。 .NetFinalize()。,IDisposableDispose()。Dispose(),Finalize,。Dispose()。.Net,,,。   ,.Net,,。,,,System.GC.Collect() ,。,,,Dispose(),System.GC.SupressFinalize(),。。   .NET,C#,--!     .NET,C#C++,--。C#delete!,:,,,。: using System; class MyClass1 {     ~MyClass1()     {         Console.WriteLine("MyClass1's destructor");     } } class MyClass2: MyClass1 {     ~MyClass2()     {         Console.WriteLine("MyClass2's destructor");     } } public class Test {     public static void Main()     {         MyClass2 MyObject = new MyClass2();         MyObject = null;         GC.Collect();         GC.WaitForPendingFinalizers();     } }   : MyClass2's destructor MyClass1's destructor   。 GC.Collect()。GC.WaitForPendingFinalizers()(Finalizaion)。(Finalizaion),。   ,,。,,Finalize。,。,。   ?.Net(Finalizaion)。 .Net,,--C#。: using System; class MyClass1 {     ~MyClass1()     {         Console.WritleLine("MyClass1 Destructor");     } }   ,,: using System; class MyClass1 {     protected override void Finalize()     {         try         { Console.WritleLine("My Class1 Destructor");         }         finally         {             base.Finalize();         }     } }   C#Finalize--Finalize(C#,System.Object,Finalize),。: using System; class MyClass {     override protected void Finalize() {}// public void MyMethod()     {         this.Finalize();// } }   : using System; class MyClass {     public void Finalize()     {         Console.WriteLine("My Class Destructor");     } } public class Test {     public static void Main()     {         MyClass MyObject=new MyClass();         MyObject.Finalize();     } }   Finalize“”,C#。System.ObjectFinalize,!   .Net,。(Finalizer),,。,,,。,。。,,。,,。,。 .Net,AB,,AA。 .NetFinalize()。,IDisposableDispose()。Dispose(),Finalize,。Dispose()。.Net,,,。   ,.Net,,。,,,System.GC.Collect() ,。,,,Dispose(),System.GC.SupressFinalize(),。。   .NET,C#,--!     .NET,C#C++,--。C#delete!,:,,,。: using System; class MyClass1 {     ~MyClass1()     {         Console.WriteLine("MyClass1's destructor");     } } class MyClass2: MyClass1 {     ~MyClass2()     {         Console.WriteLine("MyClass2's destructor");     } } public class Test {     public static void Main()     {         MyClass2 MyObject = new MyClass2();         MyObject = null;         GC.Collect();         GC.WaitForPendingFinalizers();     } }   : MyClass2's destructor MyClass1's destructor   。 GC.Collect()。GC.WaitForPendingFinalizers()(Finalizaion)。(Finalizaion),。   ,,。,,Finalize。,。,。   ?.Net(Finalizaion)。 .Net,,--C#。: using System; class MyClass1 {     ~MyClass1()     {         Console.WritleLine("MyClass1 Destructor");     } }   ,,: using System; class MyClass1 {     protected override void Finalize()     {         try         { Console.WritleLine("My Class1 Destructor");         }         finally         {             base.Finalize();         }     } }   C#Finalize--Finalize(C#,System.Object,Finalize),。: using System; class MyClass {     override protected void Finalize() {}// public void MyMethod()     {         this.Finalize();// } }   : using System; class MyClass {     public void Finalize()     {         Console.WriteLine("My Class Destructor");     } } public class Test {     public static void Main()     {         MyClass MyObject=new MyClass();         MyObject.Finalize();     } }   Finalize“”,C#。System.ObjectFinalize,!   .Net,。(Finalizer),,。,,,。,。。,,。,,。,。 .Net,AB,,AA。 .NetFinalize()。,IDisposableDispose()。Dispose(),Finalize,。Dispose()。.Net,,,。   ,.Net,,。,,,System.GC.Collect() ,。,,,Dispose(),System.GC.SupressFinalize(),。。   .NET,C#,--!     .NET,C#C++,--。C#delete!,:,,,。: using System; class MyClass1 {     ~MyClass1()     {         Console.WriteLine("MyClass1's destructor");     } } class MyClass2: MyClass1 {     ~MyClass2()     {         Console.WriteLine("MyClass2's destructor");     } } public class Test {     public static void Main()     {         MyClass2 MyObject = new MyClass2();         MyObject = null;         GC.Collect();         GC.WaitForPendingFinalizers();     } }   : MyClass2's destructor MyClass1's destructor   。 GC.Collect()。GC.WaitForPendingFinalizers()(Finalizaion)。(Finalizaion),。   ,,。,,Finalize。,。,。   ?.Net(Finalizaion)。 .Net,,--C#。: using System; class MyClass1 {     ~MyClass1()     {         Console.WritleLine("MyClass1 Destructor");     } }   ,,: using System; class MyClass1 {     protected override void Finalize()     {         try         { Console.WritleLine("My Class1 Destructor");         }         finally         {             base.Finalize();         }     } }   C#Finalize--Finalize(C#,System.Object,Finalize),。: using System; class MyClass {     override protected void Finalize() {}// public void MyMethod()     {         this.Finalize();// } }   : using System; class MyClass {     public void Finalize()     {         Console.WriteLine("My Class Destructor");     } } public class Test {     public static void Main()     {         MyClass MyObject=new MyClass();         MyObject.Finalize();     } }   Finalize“”,C#。System.ObjectFinalize,!   .Net,。(Finalizer),,。,,,。,。。,,。,,。,。 .Net,AB,,AA。 .NetFinalize()。,IDisposableDispose()。Dispose(),Finalize,。Dispose()。.Net,,,。   ,.Net,,。,,,System.GC.Collect() ,。,,,Dispose(),System.GC.SupressFinalize(),。。   .NET,C#,--!     .NET,C#C++,--。C#delete!,:,,,。: using System; class MyClass1 {     ~MyClass1()     {         Console.WriteLine("MyClass1's destructor");     } } class MyClass2: MyClass1 {     ~MyClass2()     {         Console.WriteLine("MyClass2's destructor");     } } public class Test {     public static void Main()     {         MyClass2 MyObject = new MyClass2();         MyObject = null;         GC.Collect();         GC.WaitForPendingFinalizers();     } }   : MyClass2's destructor MyClass1's destructor   。 GC.Collect()。GC.WaitForPendingFinalizers()(Finalizaion)。(Finalizaion),。   ,,。,,Finalize。,。,。   ?.Net(Finalizaion)。 .Net,,--C#。: using System; class MyClass1 {     ~MyClass1()     {         Console.WritleLine("MyClass1 Destructor");     } }   ,,: using System; class MyClass1 {     protected override void Finalize()     {         try         { Console.WritleLine("My Class1 Destructor");         }         finally         {             base.Finalize();         }     } }   C#Finalize--Finalize(C#,System.Object,Finalize),。: using System; class MyClass {     override protected void Finalize() {}// public void MyMethod()     {         this.Finalize();// } }   : using System; class MyClass {     public void Finalize()     {         Console.WriteLine("My Class Destructor");     } } public class Test {     public static void Main()     {         MyClass MyObject=new MyClass();         MyObject.Finalize();     } }   Finalize“”,C#。System.ObjectFinalize,!   .Net,。(Finalizer),,。,,,。,。。,,。,,。,。 .Net,AB,,AA。 .NetFinalize()。,IDisposableDispose()。Dispose(),Finalize,。Dispose()。.Net,,,。   ,.Net,,。,,,System.GC.Collect() ,。,,,Dispose(),System.GC.SupressFinalize(),。。   .NET,C#,--!     .NET,C#C++,--。C#delete!,:,,,。: using System; class MyClass1 {     ~MyClass1()     {         Console.WriteLine("MyClass1's destructor");     } } class MyClass2: MyClass1 {     ~MyClass2()     {         Console.WriteLine("MyClass2's destructor");     } } public class Test {     public static void Main()     {         MyClass2 MyObject = new MyClass2();         MyObject = null;         GC.Collect();         GC.WaitForPendingFinalizers();     } }   : MyClass2's destructor MyClass1's destructor   。 GC.Collect()。GC.WaitForPendingFinalizers()(Finalizaion)。(Finalizaion),。   ,,。,,Finalize。,。,。   ?.Net(Finalizaion)。 .Net,,--C#。: using System; class MyClass1 {     ~MyClass1()     {         Console.WritleLine("MyClass1 Destructor");     } }   ,,: using System; class MyClass1 {     protected override void Finalize()     {         try         { Console.WritleLine("My Class1 Destructor");         }         finally         {             base.Finalize();         }     } }   C#Finalize--Finalize(C#,System.Object,Finalize),。: using System; class MyClass {     override protected void Finalize() {}// public void MyMethod()     {         this.Finalize();// } }   : using System; class MyClass {     public void Finalize()     {         Console.WriteLine("My Class Destructor");     } } public class Test {     public static void Main()     {         MyClass MyObject=new MyClass();         MyObject.Finalize();     } }   Finalize“”,C#。System.ObjectFinalize,!   .Net,。(Finalizer),,。,,,。,。。,,。,,。,。 .Net,AB,,AA。 .NetFinalize()。,IDisposableDispose()。Dispose(),Finalize,。Dispose()。.Net,,,。   ,.Net,,。,,,System.GC.Collect() ,。,,,Dispose(),System.GC.SupressFinalize(),。。   .NET,C#,--!     .NET,C#C++,--。C#delete!,:,,,。: using System; class MyClass1 {     ~MyClass1()     {         Console.WriteLine("MyClass1's destructor");     } } class MyClass2: MyClass1 {     ~MyClass2()     {         Console.WriteLine("MyClass2's destructor");     } } public class Test {     public static void Main()     {         MyClass2 MyObject = new MyClass2();         MyObject = null;         GC.Collect();         GC.WaitForPendingFinalizers();     } }   : MyClass2's destructor MyClass1's destructor   。 GC.Collect()。GC.WaitForPendingFinalizers()(Finalizaion)。(Finalizaion),。   ,,。,,Finalize。,。,。   ?.Net(Finalizaion)。 .Net,,--C#。: using System; class MyClass1 {     ~MyClass1()     {         Console.WritleLine("MyClass1 Destructor");     } }   ,,: using System; class MyClass1 {     protected override void Finalize()     {         try         { Console.WritleLine("My Class1 Destructor");         }         finally         {             base.Finalize();         }     } }   C#Finalize--Finalize(C#,System.Object,Finalize),。: using System; class MyClass {     override protected void Finalize() {}// public void MyMethod()     {         this.Finalize();// } }   : using System; class MyClass {     public void Finalize()     {         Console.WriteLine("My Class Destructor");     } } public class Test {     public static void Main()     {         MyClass MyObject=new MyClass();         MyObject.Finalize();     } }   Finalize“”,C#。System.ObjectFinalize,!   .Net,。(Finalizer),,。,,,。,。。,,。,,。,。 .Net,AB,,AA。 .NetFinalize()。,IDisposableDispose()。Dispose(),Finalize,。Dispose()。.Net,,,。   ,.Net,,。,,,System.GC.Collect() ,。,,,Dispose(),System.GC.SupressFinalize(),。。   .NET,C#,--!     .NET,C#C++,--。C#dele


c # (csharp) Articles


Can't Find What You're Looking For?


Rating: Not yet rated

Comments

No comments posted.