Get the App
SLTechnology News&Howtos  ›  Development  › 

Example Analysis of deconstruction and garbage Collection in C #

Shulou Source: shulou.com Published: 2022-06-03 19:17:46 10月31日 Update

I would like to share with you the sample analysis of deconstruction and garbage collection in C#. I believe most people don't know much about it, so share this article for your reference. I hope you will learn a lot after reading this article. Let's learn about it!

The deconstruction of C # and the analysis of garbage collection examples

When reading, I wrote the example code, learned several knowledge points, and recorded them. At the same time, I find that my ability to write code by hand is relatively weak, so I still need to write more.

Using System;namespace ConsoleApplication {public class Program {public static void Main (string [] args) {Console.WriteLine ("Hello World!"); fun (); GC.Collect (); / / 4. If it is not explicitly recycled, the console does not print the recycling line. Console.WriteLine ("end!");} public static void fun () {/ / 1, there must be static An a=new A (); a.Dispose (); / / GC.Collect () / / 3. The collection here is invalid, because a has not released} public class A:IDisposable {/ * protected override void Finalize () {/ / 2. Destructing actually overloads Finalize, but cannot directly overload it. The compiler automatically rewrites Finalize, so you can only use destructors in your code. Console.WriteLine ("some one disposed!");} * / ~ A () {Console.WriteLine ("some one Finalize!");} public void Dispose () {Console.WriteLine ("some one disposed!");}

1. The output of the code is:

Hello World! Some one disposed! End! Some one Finalize!

This collection is very much like an asynchronous call!

2. The Dispose method of the IDisposable interface is designed to "call the object manually when the user is no longer using it"

Such as file opening, database access and so on. It needs to be released in time.

Obviously this concept and garbage collection are two concepts. After release, the object still exists and needs to be recycled by the running environment, as shown in the last line.

The above is all the content of the article "sample Analysis of deconstruction and garbage Collection in C#". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

Tags: Garbage code caching analysis articles examples content objects concepts knowledge obvious one line not so much two examples functions and at the same time most of them instance analysis Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno NVidia Xiaomi Microsoft Huawei OPPO Reno