English 中文(简体)
C#:在实施可变界面时,处置方法的内容是什么
原标题:C#:What should be the content of the Dispose method when implementing IDisposable interface

我创建了一个实施IDisposable接口的班子,而光学观测台为我提供了处置方法。 我想知道,在处置方法中应当写什么法典,以便它能够照顾我的记忆管理,或者它应该做什么。

public class ESNVerification : IDisposable 
{ 

  public bool Save(string a,string b)
  {
    //Save the data to table
    return true;
  }

  public void Dispose()
  {
       throw new NotImplementedException();
       // Really what i should do here  ?
  }
}
问题回答

页: 1 《关于执行的三项简单规则》 (b) 简化:

  • Rule 1: Don t do it (unless you need to). There are only two situations when IDisposable does need to be implemented: The class owns an unmanaged resource or The class owns managed (IDisposable) resources.
  • Rule 2: For a class owning managed resources, implement IDisposable (but not a finalizer). This implementation of IDisposable should only call Dispose for each owned resource. The class should not have a finalizer.
  • Rule 3: For a class owning a single unmanaged resource, implement both IDisposable and a finalizer.

除非你重新利用需要清理的某种未经管理的资源(在这种极其简单的情况下,你再说不),否则你就没有理由执行<代码>。 IDisposable。

The first line in the MSDN Description of the IDisposable Interface:

The primary use of this interface is to release unmanaged resources

如果你使用未经管理的资源,那么处置方法就是确保适当释放这些资源(垃圾收集者将照顾到你管理的所有资源)。

处置方法就是这样,你就可以释放你分配的并非简单目标的资源(通过垃圾收集清理)。 例如

  • streams
  • database connections and/or result sets
  • pointers to unmanaged objects
  • any object you have allocated inside your object that is also implementing IDisposable

应在您的处置方法中加以清理(或许可以通过要求对这些物体进行处置)。

如果你没有这种资源,你可能就不需要执行可支配的身份证。 然而,你可能会实施另一个接口,继承来自可识别的,例如电子计算仪。 在这种情况下,如果你没有上述任何资源,你就能够把处置方法空出。

处置是指释放未经管理的资源(即垃圾收集器在使用后不会自动得到的资源)。 共同的例子有:

该网页有重要警告,处置方法的实施方式不应有多种电话。 这是因为它被垃圾收集器使用,它可以多次使用处置方法。

在99%的案例中,Microsoft s框架大为克服,正确做法简单:

  1. If your class has any fields of types that implement IDisposable, and nobody s going to expect to use those objects once you re done with them, you should implement IDisposable, and your disposal method should call Dispose on all such fields.
  2. If your class has no such fields, but you think classes that derive from yours might, or if your class needs to implement an interface like IEnumerator(of T) which requires IDisposable, you should have an overridable Dispose method that does nothing.
  3. The proper semantics for a "dispose" method is for an object to do whatever is necessary to clean up other objects before it is abandoned. If an object doesn t have to clean up other objects, Dispose should harmlessly do nothing. There is never any reason to throw NotImplementedException or NotSupportedException from Dispose.

执行可转让的识别资料的关键点是,清理任何特定类型的“资源”,而是确保如果系统的一个物体改动 ,其他>/i” 实体需要一段时间清理,这些实体将获得清理,而这样做所需的信息和动力仍然存在。 理想的情况是,这种清理应尽快进行,但更早。 如果某一物体包含如一阵列,则无需清理。 扼杀不需要任何清理;一系列不需要清理的物体不需要任何清理,而只持有未要求清理的其他物体的物体也需要清理。 另一方面,像开张笔记本这样的行动,需要确保采取某种清理行动(填表)。 如果某一物体打开了提纲并保存了有关材料,则要求对该物体进行处置的目的就是要销毁有关袖珍的资料(将由垃圾收集者负责),但要确保在提纲上开展必要的“客户”行动。

http://msdn.microsoft.com/en-us/library/fs2xkftw.aspx”rel=“nofollow” 执行《清洁无管理资源最终确定和处置。 指出第一点是:

对于只使用管理资源(如阵列)的类型,实施处置方法没有成效,因为垃圾收集器自动收回。

d 我建议大家阅读,决定是否需要执行,并作为开端使用该守则的例子。

执行<代码>可转让<>>><>>>>>>>>>>><1>的仅有两个理由,<代码>Dispose方法中你做些什么取决于对您的情况。

如果您的班级创立了执行<代码>可转让<>代码/代码>的物体,并且无法以产生这些物体的方法加以处置,该班级将有成员变量,参照这些情况。 该类别还应实施<代码>可支配,在其中,每一可支配成员应使用<代码>Dispose。

如果你直接使用未经管理的资源,那么你的班子应当实施<编码>可支配<>><>代码>,并在<代码>Dispose中按需要处理。 确切地说,这意味着会有很大差异,因为没有标准接口。 http://www.un.org/Depts/DGACM/index_chinese.htm (此处见上文)

<>1> 页: 1 (尽管有>> are debatable)

在您的班级使用未经管理的系统资源时,作为档案和点人处理时,应采用可变模式。 释放他们使用的记忆。

If your class doesn t use unmanaged resources, it probably don t need to use this pattern.

如果你真的需要,可以:

public void Dispose()
{
  //Free here your resources

  this.Dispose(true);
  GC.SuppressFinalize(this);
}




相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...