如何在实现 IDisposable 接口的类中处置具有局部变量的对象

发布于 2025-01-11 05:13:18 字数 1711 浏览 0 评论 0原文

public class AnswerModel: IDisposable
    {
        private bool disposedValue;

        public string Answer { get; set; }
        public string CodeSnippet { get; set; }
        public string AnswerBy { get; set; }
        public DateTime DatePosted { get; set; }

        public string TimeStatus { get; set; }

        protected virtual void Dispose(bool disposing)
        {
            if (!disposedValue)
            {
                if (disposing)
                {
                    // TODO: dispose managed state (managed objects)
                }

                // TODO: free unmanaged resources (unmanaged objects) and override finalizer
                // TODO: set large fields to null
                disposedValue = true;
            }
        }

        // // TODO: override finalizer only if 'Dispose(bool disposing)' has code to free unmanaged resources
        ~AnswerModel()
        {
            // Do not change this code. Put cleanup code in 'Dispose(bool disposing)' method
            Dispose(disposing: false);
        }

        public void Dispose()
        {
            // Do not change this code. Put cleanup code in 'Dispose(bool disposing)' method
            Dispose(disposing: true);
            GC.SuppressFinalize(this);
        }
    }




  using (AnswerModel a = new AnswerModel())
                    {
                        a.Answer = answer.Answered;
                        a.AnswerBy = answer.AnswerBy;
                        a.DatePosted = answer.DatePosted;
                        a.CodeSnippet = answer.CodeSnippet;
                        answerModel.Add(a);
                    }

如何处理AnswerModel实例中的变量?

它会自行发生吗?

public class AnswerModel: IDisposable
    {
        private bool disposedValue;

        public string Answer { get; set; }
        public string CodeSnippet { get; set; }
        public string AnswerBy { get; set; }
        public DateTime DatePosted { get; set; }

        public string TimeStatus { get; set; }

        protected virtual void Dispose(bool disposing)
        {
            if (!disposedValue)
            {
                if (disposing)
                {
                    // TODO: dispose managed state (managed objects)
                }

                // TODO: free unmanaged resources (unmanaged objects) and override finalizer
                // TODO: set large fields to null
                disposedValue = true;
            }
        }

        // // TODO: override finalizer only if 'Dispose(bool disposing)' has code to free unmanaged resources
        ~AnswerModel()
        {
            // Do not change this code. Put cleanup code in 'Dispose(bool disposing)' method
            Dispose(disposing: false);
        }

        public void Dispose()
        {
            // Do not change this code. Put cleanup code in 'Dispose(bool disposing)' method
            Dispose(disposing: true);
            GC.SuppressFinalize(this);
        }
    }




  using (AnswerModel a = new AnswerModel())
                    {
                        a.Answer = answer.Answered;
                        a.AnswerBy = answer.AnswerBy;
                        a.DatePosted = answer.DatePosted;
                        a.CodeSnippet = answer.CodeSnippet;
                        answerModel.Add(a);
                    }

How to dispose the variables in the AnswerModel instance?

Does it happen by itself?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

月下凄凉 2025-01-18 05:13:18

C# 是一种内存管理语言。当没有对该对象的引用并且发生 GC 循环(GC = 垃圾收集器)时,在堆上创建的对象将自动被垃圾收集。

然而,有些对象使用托管内存之外的资源,例如文件流、Web 连接或数据库连接。它们实现了一个名为 IDisposable 的接口,并带有一个名为 Dispose() 的函数,您可以在使用完资源后调用该函数。

此外,~Object() Finalizers 不是一个好的做法,因为您不知道 GC 何时释放对象,并且它可能会创建您无法控制的负载。

总之,当您的对象使用需要 Dispose 的外部资源时,您应该实现 IDisposable,这样您就可以在不再需要这些资源时释放它们。

C# is a memory managed language. Objects created on the heap will get garbage collected automatically when there is no reference to the object and a GC cycle occurs (GC = garbage collector).

However there are some object that use resources outside the managed memory, For example File Streams, Web Connections or Database connection. They implement an interface called IDisposable with a function called Dispose(), that you can call when you finish using the resource.

In addition ~Object() Finalizers are not good practice, because you don't know exactly when the GC frees the object, and it can create a load that you cannot control.

In conclusion you should implement IDisposable when your object uses outside resources that need to be Disposed, so you can free the resource when you don't need them anymore.

睫毛上残留的泪 2025-01-18 05:13:18
public class AnswerModel
    {
        private bool disposedValue;

        public string Answer { get; set; }
        public string CodeSnippet { get; set; }
        public string AnswerBy { get; set; }
        public DateTime DatePosted { get; set; }

        public string TimeStatus { get; set; }
    }

   ......somewhere within a function call.
     AnswerModel a = new AnswerModel()
     a.Answer = answer.Answered;
     a.AnswerBy = answer.AnswerBy;
     a.DatePosted = answer.DatePosted;
     a.CodeSnippet = answer.CodeSnippet;
     answerModel.Add(a);

               
                       

这个实例是否会在没有任何终结器的情况下被垃圾收集?

public class AnswerModel
    {
        private bool disposedValue;

        public string Answer { get; set; }
        public string CodeSnippet { get; set; }
        public string AnswerBy { get; set; }
        public DateTime DatePosted { get; set; }

        public string TimeStatus { get; set; }
    }

   ......somewhere within a function call.
     AnswerModel a = new AnswerModel()
     a.Answer = answer.Answered;
     a.AnswerBy = answer.AnswerBy;
     a.DatePosted = answer.DatePosted;
     a.CodeSnippet = answer.CodeSnippet;
     answerModel.Add(a);

               
                       

Is this instance going to be garbage collected without any finalizers?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文