【问题标题】:Shared.CellItem' does not implement interface member 'System.IDisposable.Dispose()Shared.CellItem' 没有实现接口成员 'System.IDisposable.Dispose()
【发布时间】:2013-08-23 10:07:40
【问题描述】:

我是 C# 新手,我正在尝试处理占用大量内存的对象,因为我已经运行了内存分析并且需要处理一些资源并调用 GC 的 finalize 方法。但是 IDisposable 无法实现我的课程,这是为什么呢?我应该如何为我的班级实施 IDispose?

 public class CellItem: IDisposable
        {
            public int MedicationDispenseId { get; set; }
            public Enumerations.Timeslot Timeslot { get; set; }
            public DateTime DateAdministered { get; set; }

            public void dispose() {

                if (this.MedicationDispenseId != null ) {
                    this.dispose();

                }
                if (this.Timeslot != null)
                {
                    this.dispose();

                }
                if (this.DateAdministered != null)
                {
                    this.dispose();

                }
            }

        }

【问题讨论】:

  • 即使您更正了大小写,在Dispose 中调用this.Dispose 也是一个坏主意。 intDateTime 不实现 IDisposable,所以你可能根本不应该在 CellItem 上实现 IDisposable
  • 是的。谢谢 经过一番阅读,我得出了同样的结论。

标签: c# memory garbage-collection


【解决方案1】:

C# 区分大小写,您希望以大写字母开头的方法命名,即

dispose => Dispose

还可以查看Implement IDisposable correctlyyet another good answer from John Skeet

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-05-08
    • 2015-06-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多