如何在实现 IDisposable 接口的类中处置具有局部变量的对象
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
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 calledDispose()
, 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 beDisposed
, so you can free the resource when you don't need them anymore.这个实例是否会在没有任何终结器的情况下被垃圾收集?
Is this instance going to be garbage collected without any finalizers?