Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Example Analysis of deconstruction and garbage Collection in C #

2025-03-09 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

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!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report