Should I dispose the IDisposable given to me?

Possible Duplicate: Who should call Dispose on IDisposable objects when passed into another object? Say you have a class with an interface like this: public interface Foo { Load(IDisposable something); } When implementing this method, should I call dispose when done? In other words, when a method of a class takes a Stream , Reader or anything else which is IDisposable , should this met

我应该处置给我的IDisposable吗?

可能重复: 当传入另一个对象时,谁应该在IDisposable对象上调用Dispose? 假设你有一个类似这样的接口的类: public interface Foo { Load(IDisposable something); } 在实施这种方法时,我应该在完成后调用处置吗? 换句话说,当一个类的方法需要一个Stream , Reader或者其他任何一个IDisposable ,这个方法是否应该处理这个流,或者这应该留给谁来调用这个方法? 我知道任何一种方式都可以工作,只是好奇别人更

Why would a class implement IDisposable explicitly instead of implicitly?

I was using the FtpWebResponse class and didn't see a Dispose method. It turns out that the class implements IDisposable, but does so explicitly so that you must first cast your instance to IDisposable before calling Dispose: // response is an instance of FtpWebResposne ((IDisposable) response).Dispose(); Why would the designer of a class such as this one choose to implement IDisposable ex

为什么类会显式而不是隐式地实现IDisposable?

我正在使用FtpWebResponse类,但没有看到Dispose方法。 事实证明,该类实现了IDisposable,但是这样做是明确的,因此在调用Dispose之前,必须先将实例转换为IDisposable: // response is an instance of FtpWebResposne ((IDisposable) response).Dispose(); 为什么这样的类的设计者会选择明确地实现IDisposable? 正如Anthony Pegram所说,以这种方式做事掩盖了这样一个事实,即应该为普通开发人员处理该对象,因为他/她每

Dispose vs Dispose(bool)

I am confused about dispose. I am trying to get my code disposing resources correctly. So I have been setting up my classes as IDisposable (with a Dispose method) them making sure that the Dispose method gets called. But now FXCop is telling me lots of stuff about Disposing = false and calling Dispose(false). I don't see a Dispose method that takes a bool. Do I need to make one? If so

Dispose vs Dispose(布尔)

我对处置感到困惑。 我试图让我的代码正确地处理资源。 所以我一直将我的类设置为IDisposable(使用Dispose方法),确保Dispose方法被调用。 但是现在FXCop告诉我很多关于Disposing = false和Dispose(false)的东西。 我没有看到一个需要布尔的Dispose方法。 我需要制作一个吗? 如果是这样,为什么? 为什么不只是有一个方法在处置时被调用? 我在这里看到了一些代码:http://msdn.microsoft.com/en-us/library/ms2

Using keyword and Managed\UnManaged code

C# keyword Using implements Idisposable which provides a mechanism for releasing unmanaged resources. Now i was going through this code string txt = String.Empty; using (StreamReader sr = new StreamReader(filename)) { txt = sr.ReadToEnd(); } and cant stop wondering, why is the keyword Using is used in this code while StreamReader is a Managed resource and it is the respon

使用关键字和托管\ UnManaged代码

C#关键字使用实现Idisposable它提供了释放非托管资源的机制。 现在我正在浏览这个代码 string txt = String.Empty; using (StreamReader sr = new StreamReader(filename)) { txt = sr.ReadToEnd(); } 并不能停止想知道,为什么在这段代码中使用关键字Using ,而StreamReader是一个托管资源,它的作用域是垃圾收集器在其作用域结束后释放对象内存的责任。 所以我的问题是, 上面的代码只是一个明

Clean Up Vs Memory Reclaim in .Net

I was reading this MSDN reference: Although the garbage collector is able to track the lifetime of an object that encapsulates an unmanaged resource, it does not have specific knowledge about how to clean up the resource. For these types of objects, the .NET Framework provides the Object.Finalize method, which allows an object to clean up its unmanaged resources properly when the garbage colle

在.Net中清理Vs内存回收

我正在阅读这个MSDN参考资料: 虽然垃圾收集器能够跟踪封装非托管资源的对象的生命周期,但它没有关于如何清理资源的具体知识。 对于这些类型的对象,.NET Framework提供了Object.Finalize方法,该方法允许对象在垃圾回收器回收对象使用的内存时正确地清理其非托管资源。 默认情况下,Finalize方法什么也不做。 如果您希望垃圾回收器在回收对象内存之前对对象执行清理操作,则必须重写类中的Finalize方法。 我了解GC的工

What is the difference in managed and unmanaged code, memory and size?

After seeing and listening a lot regarding managed and unmanaged code, and knowing the only difference is that managed is about CLR and un-managed is outside of the CLR, it makes me really curious to know it in detail. What is it all about, managed and unmanaged code, memory and size? How can code I write in C# be unmanaged while this is C# code and how does a memory of size becomes unmanaged.

托管和非托管代码,内存和大小有什么不同?

在查看并听取了很多关于托管代码和非托管代码的信息之后,了解到唯一的区别是托管的是关于CLR的,而未托管的则不在CLR之中,这让我非常好奇,要详细了解它。 这是什么,托管和非托管代码,内存和大小? 在C#代码中,我如何用C#编写的代码处于非托管状态,以及内存大小如何变得不受管理。 一个例子和一点洞察力将会有所帮助。 简短的回答: 托管代码是您编写和编译为.NET CIL的.NET代码(VB.NET,C#等)。 非托管代

How do you prevent IDisposable from spreading to all your classes?

Start with these simple classes... Let's say I have a simple set of classes like this: class Bus { Driver busDriver = new Driver(); } class Driver { Shoe[] shoes = { new Shoe(), new Shoe() }; } class Shoe { Shoelace lace = new Shoelace(); } class Shoelace { bool tied = false; } A Bus has a Driver , the Driver has two Shoe s, each Shoe has a Shoelace . All very silly.

你如何防止IDisposable传播给你所有的课程?

从这些简单的课程开始...... 假设我有一组简单的类: class Bus { Driver busDriver = new Driver(); } class Driver { Shoe[] shoes = { new Shoe(), new Shoe() }; } class Shoe { Shoelace lace = new Shoelace(); } class Shoelace { bool tied = false; } Bus有一个Driver , Driver有两个Shoe ,每个Shoe都有Shoelace 。 非常愚蠢。 向鞋带添加一个IDisposable对象 后来我决定Shoelace上的一

What is the difference between using IDisposable vs a destructor in C#?

When would I implement IDispose on a class as opposed to a destructor? I read this article, but I'm still missing the point. My assumption is that if I implement IDispose on an object, I can explicitly 'destruct' it as opposed to waiting for the garbage collector to do it. Is this correct? Does that mean I should always explicitly call Dispose on an object? What are some common

使用IDisposable与C#中的析构函数有什么区别?

我什么时候在类上实现IDispose而不是析构函数? 我读过这篇文章,但我仍然忽略了这一点。 我的假设是,如果我在一个对象上实现IDispose,我可以明确地“销毁”它而不是等待垃圾收集器去做。 它是否正确? 这是否意味着我应该始终明确地在对象上调用Dispose? 这有什么常见的例子? 终结器(aka析构函数)是垃圾回收(GC)的一部分 - 当(或者甚至)发生这种情况时它是不确定的,因为GC主要是由于内存压力(即需要更多空间

Minimal IDisposable implimenation for managed resources only

There is a LOT of info around about the "standard full" IDisposable implementation for disposing of unmanaged resources - but in reality this case is (very) rare (most resources are already wrapped by managed classes). This question focuses on a mimimal implementation of IDisposable for the much more common "managed resources only" case. 1: Is the mimimal implementation of

仅用于受管资源的最小IDisposable启示

关于处理非托管资源的“标准完整” IDisposable实现有很多信息 - 但实际上这种情况非常罕见(大多数资源已被托管类包装)。 这个问题关注于IDisposable的一个简单实现,用于更常见的“仅管理资源”情况。 1:以下代码中IDisposable的最小实现是否正确,是否存在问题? 2:是否有任何理由添加完整的标准IDisposable实现( Dispose() , Dispose(bool) , Finalizer等)以提供最小的实现? 3:在这个最小的情况下可以/明智地使D

Implementing IDisposable correctly

In my classes I implement IDisposable as follows: public class User : IDisposable { public int id { get; protected set; } public string name { get; protected set; } public string pass { get; protected set; } public User(int UserID) { id = UserID; } public User(string Username, string Password) { name = Username; pass = Password; }

正确实施IDisposable

在我的课程中,我实现了IDisposable,如下所示: public class User : IDisposable { public int id { get; protected set; } public string name { get; protected set; } public string pass { get; protected set; } public User(int UserID) { id = UserID; } public User(string Username, string Password) { name = Username; pass = Password; } //