简体   繁体   English

GC.Collect() 在 Dispose 中简单 class

[英]GC.Collect() in Dispose in simple class

I have inherited a kind of ancient website project in c#.我在c#中继承了一种古老的网站项目。 It originates from 2003 This has all over the place simple classes defined that inherit from IDisposible and implement a Dispose() method where GC.Collect() is called as below:它起源于 2003 年,它到处都有定义的简单类,这些类继承自 IDisposible 并实现了一个 Dispose() 方法,其中 GC.Collect() 被调用如下:

public class ProjectAutostart : IDisposable
{
    public void Dispose()
    {
        Dispose(true);
        GC.Collect();
    }
    protected virtual void Dispose(bool disposing) { }
    private Int32 _id;
    private Int32 _stepid;
    private Int64 _stepcounter;

    public Int32 ID
    {
        set { _id = value; }
        get { return _id; }
    }
    public Int32 StepID
    {
        set { _stepid = value; }
        get { return _stepid; }
    }
    public Int64 StepCounter
    {
        set { _stepcounter = value; }
        get { return _stepcounter; }
    }
}

These classes are called like:这些类被称为:

List<Projects.ProjectAutostart> ProjectList = DataLayer.Projects.getProjectAutoStart();

Which ends up in:最终出现在:

public static List<Projects.ProjectAutostart> getProjectAutoStart()
{
    List<Projects.ProjectAutostart> Projects = new List<Projects.ProjectAutostart>();
    DataTable DataTable = SQL.DataTable("getProjectAutoStart", null);
    foreach (DataRow dt in DataTable.Rows)
    {
        Projects.Add(new ProjectAutostart { ID = Convert.ToInt32(dt["projectid"]), StepID = Convert.ToInt32(dt["stepid"]), StepCounter = Convert.ToInt32(e["autostartstepcounter"]) });
    }
    DataTable.Dispose();
    return Projects;
}

I'm not experienced with this type of projects, I'm totally into the .net core restfull area so this code is strange to me.我对这类项目没有经验,我完全进入了 .net 核心休息区,所以这段代码对我来说很奇怪。 Asides from the total weird way of implementing, These GC.Collect() and that Dispose() feel totally useless since it is managed code and it are simple class without any coding executing.除了完全奇怪的实现方式之外,这些 GC.Collect() 和 Dispose() 感觉完全没用,因为它是托管代码,而且它很简单 class 没有任何编码执行。 Why would someone put that Dispose and GC.Collect() in there?为什么有人会把 Dispose 和 GC.Collect() 放在那里? Should I just remove it?我应该删除它吗?

As already noted in the comments, GC.Collect() in the Dispose method is completely superfluous here.正如评论中已经指出的,Dispose 方法中的GC.Collect()在这里完全是多余的。 Maybe they got the dispose pattern wrong in the first place.也许他们一开始就弄错了处置模式。

The recommended implementation for the dispose pattern is as follows: dispose 模式的推荐实现如下:

    public void Dispose()
    {
        Dispose(true);
        GC.SuppressFinalize(this); // sic!
    }
    protected virtual void Dispose(bool disposing) 
    {
    }

However, since your ProjectAutostart class is a pure DTO and does not have any disposable fields (and any derived classes probably don't, either), you don't even need to implement dispose here.但是,由于您的ProjectAutostart class 是纯 DTO 并且没有任何一次性字段(并且任何派生类也可能没有),因此您甚至不需要在这里实现 dispose。 Just remove both Dispose methods as well as the interface declaration.只需删除Dispose方法和接口声明即可。 What you do need to dispose is the DataTable object, but that's properly done already.需要处理的是DataTable object,但这已经正确完成。

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

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