简体   繁体   English

C#秒表类

[英]C# stopwatch Class

I'm pretty new to programming so would just like to ask the following question regarding the .net built in stopwatch class using system.diagnostics in C#. 我对编程很陌生,所以只想问以下有关在C#中使用system.diagnostics建立在秒表类中的.net的问题。

Does it automatically take care of garbage collection each time it is called or would I still have to implement garbage collection? 它是在每次调用时自动处理垃圾回收吗?还是我仍然必须实现垃圾回收?

Regards 问候

You never have to implement garbage collection. 您无需实现垃圾回收。

You may have to manage resources. 您可能必须管理资源。 For that, check whether a Type implements IDisposable . 为此,请检查Type是否实现IDisposable
And when it does, use the instance in a using() {} block or implement IDisposable on your own class. 然后,在using() {}块中使用实例,或者在您自己的类上实现IDisposable

But the Stopwatch class is not IDisposable so in this case, no action is required. 但是Stopwatch类不是IDisposable因此在这种情况下,无需执行任何操作。

如果您的意思是垃圾回收,那就是在使用变量,方法等之后整理内存的过程,所有.Net语言(甚至C#)都可以帮您解决这个问题。

You never have to implement garbage collection. 您无需实现垃圾回收。 That's the point if it. 这就是重点。

If the class implements IDisposable then you can manage it resource wise with something like 如果该类实现了IDisposable,那么您可以通过以下方式在资源上进行明智的管理:

using(MyDisposable myDisposable = new MyDisposable())
{
   myDisposable.DoSomething();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM